MediatR.Azure.EventGrid
1.1.1
dotnet add package MediatR.Azure.EventGrid --version 1.1.1
NuGet\Install-Package MediatR.Azure.EventGrid -Version 1.1.1
<PackageReference Include="MediatR.Azure.EventGrid" Version="1.1.1" />
paket add MediatR.Azure.EventGrid --version 1.1.1
#r "nuget: MediatR.Azure.EventGrid, 1.1.1"
// Install MediatR.Azure.EventGrid as a Cake Addin #addin nuget:?package=MediatR.Azure.EventGrid&version=1.1.1 // Install MediatR.Azure.EventGrid as a Cake Tool #tool nuget:?package=MediatR.Azure.EventGrid&version=1.1.1
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
EventGridEvent
objects, theEventType
andDataVersion
properties are used to resolve the .NET type. - For
CloudEvent
objects, theType
andDataSchema
properties are used to resolve the .NET type.
To register a custom data type, use the DataTypes.Add
method on the EventGridMediatorBuilder
:
services.AddEventGridMediator(builder =>
{
builder.DataTypes
.Add<MyCustomEventData>(nameof(MyCustomEventData))
.Add<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.DataTypes.RegisterFromAssembly(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);
});
Publishing Events to Event Grid
If you don't want to have a dependency on the MediatR
package, you can still publish events to Event Grid by using the MediatR.Azure.EventGrid.Serialization
package on its own.
To publish EventGridEvent
objects using the default serializer used by EventGridMediator
to resolve the data type, use the following code (the example is an ASP.NET Core Minimal API):
app.MapPost("/api/endpoint", async (HttpContext context, CancellationToken cancellationToken) =>
{
var eventData = new MyCustomEventData();
var factory = context.RequestServices.GetRequiredService<EventGridEventFactory>();
var client = context.RequestServices.GetRequiredService<EventGridPublisherClient>();
var eventGridEvent = factory.Create("subject", eventData);
await client.SendEventAsync(eventGridEvent, cancellationToken);
});
public record MyCustomEventData { }
To publish `CloudEvent objects, use the following code (the example is an ASP.NET Core Minimal API):
app.MapPost("/api/endpoint", async (HttpContext context, CancellationToken cancellationToken) =>
{
var eventData = new MyCustomEventData();
var factory = context.RequestServices.GetRequiredService<CloudEventFactory>();
var client = context.RequestServices.GetRequiredService<EventGridPublisherClient>();
var cloudEvent = factory.Create("source", eventData);
await client.SendEventAsync(cloudEvent, cancellationToken);
});
public record MyCustomEventData { }
Handling Events
By default, MediatR will invoke each INotificationHandler<T>
sequentially. 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. |
.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
- MediatR (>= 12.0.1)
- MediatR.Azure.EventGrid.Serialization (>= 1.1.1)
- 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.