MediatR.Azure.EventGrid
1.0.3
See the version list below for details.
dotnet add package MediatR.Azure.EventGrid --version 1.0.3
NuGet\Install-Package MediatR.Azure.EventGrid -Version 1.0.3
<PackageReference Include="MediatR.Azure.EventGrid" Version="1.0.3" />
<PackageVersion Include="MediatR.Azure.EventGrid" Version="1.0.3" />
<PackageReference Include="MediatR.Azure.EventGrid" />
paket add MediatR.Azure.EventGrid --version 1.0.3
#r "nuget: MediatR.Azure.EventGrid, 1.0.3"
#:package MediatR.Azure.EventGrid@1.0.3
#addin nuget:?package=MediatR.Azure.EventGrid&version=1.0.3
#tool nuget:?package=MediatR.Azure.EventGrid&version=1.0.3
Azure Event Grid Mediator
A MediatR bridge for Azure Event Grid.
Installing MediatR.Azure.EventGrid
You can install the latest version of MediatR.Azure.EventGrid from NuGet by running the following command in PowerShell:
Install-Package MediatR.Azure.EventGrid
Alternatively, you can use the .NET command line:
dotnet add package MediatR.Azure.EventGrid
Getting Started
To use EventGridMediator, you need to register it with the IServiceCollection along with MediatR:
services
.AddMediatR(mediator => mediator.RegisterServicesFromAssembly(typeof(Program).Assembly))
.AddEventGridMediator();
This will automatically deserialize the Event Grid system topics data.
Custom Events
For custom events, you need to register each data type:
- For
EventGridEventobjects, theEventTypeandDataVersionproperties are used to resolve the .NET type. - For
CloudEventobjects, theTypeandDataSchemaproperties are used to resolve the .NET type.
To register a custom data type, use the AddDataType method on the EventGridMediatorBuilder:
services.AddEventGridMediator(builder =>
{
builder
.AddDataType<MyCustomEventData>(nameof(MyCustomEventData))
.AddDataType<MyCustomEventDataV2>(nameof(MyCustomEventData), "2.0");
});
Alternatively, you can use the EventGridDataTypeAttribute attribute on classes to register them through assembly discovery of public types:
services.AddEventGridMediator(builder => builder.RegisterDataTypesFromAssembly(typeof(Program).Assembly));
[EventGridDataType(nameof(MyCustomEventData))]
public class MyCustomEventData
{
}
Publishing Events to MediatR
To publish EventGridEvent objects using the EventGridMediator, use the following code (the example is an ASP.NET Core Minimal API):
app.MapPost("/api/events", async (HttpContext context, CancellationToken cancellationToken) =>
{
var json = await BinaryData.FromStreamAsync(context.Request.Body, cancellationToken).ConfigureAwait(false);
var events = EventGridEvent.ParseMany(json);
var mediator = context.RequestServices.GetRequiredService<EventGridMediator>();
await mediator.PublishAsync(events, cancellationToken);
});
To publish CloudEvent objects, use the following code (the example is an ASP.NET Core Minimal API):
app.MapPost("/api/events", async (HttpContext context, CancellationToken cancellationToken) =>
{
var json = await BinaryData.FromStreamAsync(context.Request.Body, cancellationToken).ConfigureAwait(false);
var events = CloudEvent.ParseMany(json);
var mediator = context.RequestServices.GetRequiredService<EventGridMediator>();
await mediator.PublishAsync(events, cancellationToken);
});
Handling Events
By default, MediatR will invoke each INotificationHandler<T> in a sequential manner. However, as of MediatR 12.0.0, you can now parallelize the notification with the TaskWhenAllPublisher strategy. For more information, see pull request 838.
Regardless of the notification strategy you choose, keep in mind that handlers should be idempotent because any failures may require reprocessing of the event.
To handle EventGridEvent objects, implement IEventGridEventHandler<T>:
public record MyCustomEventData { }
public class MyCustomEventHandler : IEventGridEventHandler<MyCustomEventData>
{
public Task HandleAsync(EventGridEvent eventGridEvent, MyCustomEventData data, CancellationToken cancellationToken)
{
Debug.WriteLine($"Received event {eventGridEvent.Id} of type {eventGridEvent.EventType} with data {data}.");
return Task.CompletedTask;
}
}
To handle CloudEvent objects, implement ICloudEventHandler<T>:
public record MyCustomEventData { }
public class MyCustomEventHandler : ICloudEventHandler<MyCustomEventData>
{
public Task HandleAsync(CloudEvent cloudEvent, MyCustomEventData data, CancellationToken cancellationToken)
{
Debug.WriteLine($"Received event {cloudEvent.Id} of type {cloudEvent.Type} with data {data}.");
return Task.CompletedTask;
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Azure.Messaging.EventGrid (>= 4.13.0)
- MediatR (>= 12.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- System.Net.Http (>= 4.3.4)
- System.Text.RegularExpressions (>= 4.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.