{"id":22382577,"url":"https://github.com/smokedlinq/aemediator","last_synced_at":"2025-10-17T17:09:54.013Z","repository":{"id":65898869,"uuid":"600912833","full_name":"smokedlinq/aemediator","owner":"smokedlinq","description":"A MediatR bridge for Azure Event Grid","archived":false,"fork":false,"pushed_at":"2024-05-01T04:29:08.000Z","size":65,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-01T12:41:38.495Z","etag":null,"topics":["azure","csharp","dotnet","eventgrid","mediatr"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smokedlinq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-13T00:18:41.000Z","updated_at":"2023-10-02T19:04:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"c016c79d-2c3a-4fab-b2ef-0597a2942db4","html_url":"https://github.com/smokedlinq/aemediator","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smokedlinq%2Faemediator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smokedlinq%2Faemediator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smokedlinq%2Faemediator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smokedlinq%2Faemediator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smokedlinq","download_url":"https://codeload.github.com/smokedlinq/aemediator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239993959,"owners_count":19730779,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azure","csharp","dotnet","eventgrid","mediatr"],"created_at":"2024-12-05T00:13:32.127Z","updated_at":"2025-10-17T17:09:53.923Z","avatar_url":"https://github.com/smokedlinq.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Azure Event Grid Mediator\n\n---\n\n![CI](https://github.com/smokedlinq/aemediator/workflows/CI/badge.svg)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=smokedlinq_aemediator\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=smokedlinq_aemediator)\n[![NuGet](https://img.shields.io/nuget/dt/MediatR.Azure.EventGrid.svg)](https://www.nuget.org/packages/MediatR.Azure.EventGrid)\n[![NuGet](https://img.shields.io/nuget/vpre/MediatR.Azure.EventGrid.svg)](https://www.nuget.org/packages/MediatR.Azure.EventGrid)\n\nA [MediatR](/jbogard/MediatR) bridge for Azure Event Grid.\n\n## Installing MediatR.Azure.EventGrid\n\nYou can install the latest version of `MediatR.Azure.EventGrid` from NuGet by running the following command in PowerShell:\n\n```powershell\nInstall-Package MediatR.Azure.EventGrid\n```\n\nAlternatively, you can use the .NET command line:\n\n```dotnetcli\ndotnet add package MediatR.Azure.EventGrid\n```\n\n## Getting Started\n\nTo use `EventGridMediator`, you need to register it with the `IServiceCollection` along with MediatR:\n\n```csharp\nservices\n    .AddMediatR(mediator =\u003e mediator.RegisterServicesFromAssembly(typeof(Program).Assembly))\n    .AddEventGridMediator();\n```\n\nThis will automatically deserialize the [Event Grid system topics](https://learn.microsoft.com/azure/event-grid/system-topics) data.\n\n## Custom Events\n\nFor custom events, you need to register each data type:\n\n- For `EventGridEvent` objects, the `EventType` and `DataVersion` properties are used to resolve the .NET type.\n- For `CloudEvent` objects, the `Type` and `DataSchema` properties are used to resolve the .NET type.\n\nTo register a custom data type, use the `DataTypes.Add` method on the `EventGridMediatorBuilder`:\n\n```csharp\nservices.AddEventGridMediator(builder =\u003e\n{\n    builder.DataTypes\n        .Add\u003cMyCustomEventData\u003e(nameof(MyCustomEventData))\n        .Add\u003cMyCustomEventDataV2\u003e(nameof(MyCustomEventData), \"2.0\");\n});\n```\n\nAlternatively, you can use the `EventGridDataTypeAttribute` attribute on classes to register them through assembly discovery of public types:\n\n```csharp\nservices.AddEventGridMediator(builder =\u003e builder.DataTypes.RegisterFromAssembly(typeof(Program).Assembly));\n\n[EventGridDataType(nameof(MyCustomEventData))]\npublic class MyCustomEventData\n{\n}\n```\n\n## Publishing Events to MediatR\n\nTo publish `EventGridEvent` objects using the `EventGridMediator`, use the following code (the example is an ASP.NET Core Minimal API):\n\n```csharp\napp.MapPost(\"/api/events\", async (HttpContext context, CancellationToken cancellationToken) =\u003e\n{\n    var json = await BinaryData.FromStreamAsync(context.Request.Body, cancellationToken).ConfigureAwait(false);\n    var events = EventGridEvent.ParseMany(json);\n    var mediator = context.RequestServices.GetRequiredService\u003cEventGridMediator\u003e();\n\n    await mediator.PublishAsync(events, cancellationToken);\n});\n```\n\nTo publish `CloudEvent` objects, use the following code (the example is an ASP.NET Core Minimal API):\n\n```csharp\napp.MapPost(\"/api/events\", async (HttpContext context, CancellationToken cancellationToken) =\u003e\n{\n    var json = await BinaryData.FromStreamAsync(context.Request.Body, cancellationToken).ConfigureAwait(false);\n    var events = CloudEvent.ParseMany(json);\n    var mediator = context.RequestServices.GetRequiredService\u003cEventGridMediator\u003e();\n\n    await mediator.PublishAsync(events, cancellationToken);\n});\n```\n\n## Publishing Events to Event Grid\n\nIf 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.\n\nTo 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):\n\n```csharp\napp.MapPost(\"/api/endpoint\", async (HttpContext context, CancellationToken cancellationToken) =\u003e\n{\n    var eventData = new MyCustomEventData();\n    var factory = context.RequestServices.GetRequiredService\u003cEventGridEventFactory\u003e();\n    var client = context.RequestServices.GetRequiredService\u003cEventGridPublisherClient\u003e();\n    var eventGridEvent = factory.Create(\"subject\", eventData);\n\n    await client.SendEventAsync(eventGridEvent, cancellationToken);\n});\n\npublic record MyCustomEventData { }\n```\n\nTo publish `CloudEvent objects, use the following code (the example is an ASP.NET Core Minimal API):\n\n```csharp\napp.MapPost(\"/api/endpoint\", async (HttpContext context, CancellationToken cancellationToken) =\u003e\n{\n    var eventData = new MyCustomEventData();\n    var factory = context.RequestServices.GetRequiredService\u003cCloudEventFactory\u003e();\n    var client = context.RequestServices.GetRequiredService\u003cEventGridPublisherClient\u003e();\n    var cloudEvent = factory.Create(\"source\", eventData);\n\n    await client.SendEventAsync(cloudEvent, cancellationToken);\n});\n\npublic record MyCustomEventData { }\n```\n\n## Handling Events\n\nBy default, MediatR will invoke each `INotificationHandler\u003cT\u003e` 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](https://github.com/jbogard/MediatR/pull/838).\n\nRegardless of the notification strategy you choose, keep in mind that handlers should be idempotent because any failures may require reprocessing of the event.\n\nTo handle `EventGridEvent` objects, implement `IEventGridEventHandler\u003cT\u003e`:\n\n```csharp\npublic record MyCustomEventData { }\n\npublic class MyCustomEventHandler : IEventGridEventHandler\u003cMyCustomEventData\u003e\n{\n    public Task HandleAsync(EventGridEvent eventGridEvent, MyCustomEventData data, CancellationToken cancellationToken)\n    {\n        Debug.WriteLine($\"Received event {eventGridEvent.Id} of type {eventGridEvent.EventType} with data {data}.\");\n        return Task.CompletedTask;\n    }\n}\n```\n\nTo handle `CloudEvent` objects, implement `ICloudEventHandler\u003cT\u003e`:\n\n```csharp\npublic record MyCustomEventData { }\n\npublic class MyCustomEventHandler : ICloudEventHandler\u003cMyCustomEventData\u003e\n{\n    public Task HandleAsync(CloudEvent cloudEvent, MyCustomEventData data, CancellationToken cancellationToken)\n    {\n        Debug.WriteLine($\"Received event {cloudEvent.Id} of type {cloudEvent.Type} with data {data}.\");\n        return Task.CompletedTask;\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmokedlinq%2Faemediator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmokedlinq%2Faemediator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmokedlinq%2Faemediator/lists"}