{"id":18494272,"url":"https://github.com/workleap/wl-extensions-mediatr","last_synced_at":"2025-04-08T22:31:11.106Z","repository":{"id":74307086,"uuid":"601812509","full_name":"workleap/wl-extensions-mediatr","owner":"workleap","description":"MediatR extensions, behaviors and default configuration.","archived":false,"fork":false,"pushed_at":"2025-03-15T02:38:47.000Z","size":185,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":32,"default_branch":"main","last_synced_at":"2025-03-15T14:03:20.778Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/workleap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-14T21:52:34.000Z","updated_at":"2025-03-15T02:38:50.000Z","dependencies_parsed_at":"2023-08-09T20:32:05.128Z","dependency_job_id":"a16887ce-9bca-4545-8581-aa8131396824","html_url":"https://github.com/workleap/wl-extensions-mediatr","commit_stats":null,"previous_names":["gsoft-inc/gsoft-extensions-mediatr","workleap/wl-extensions-mediatr"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workleap%2Fwl-extensions-mediatr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workleap%2Fwl-extensions-mediatr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workleap%2Fwl-extensions-mediatr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/workleap%2Fwl-extensions-mediatr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/workleap","download_url":"https://codeload.github.com/workleap/wl-extensions-mediatr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247940050,"owners_count":21021899,"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":[],"created_at":"2024-11-06T13:18:56.964Z","updated_at":"2025-04-08T22:31:06.095Z","avatar_url":"https://github.com/workleap.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Workleap.Extensions.MediatR\n\n[![nuget](https://img.shields.io/nuget/v/Workleap.Extensions.MediatR.svg?logo=nuget)](https://www.nuget.org/packages/Workleap.Extensions.MediatR/)\n[![build](https://img.shields.io/github/actions/workflow/status/gsoft-inc/wl-extensions-mediatr/publish.yml?logo=github\u0026branch=main)](https://github.com/gsoft-inc/wl-extensions-mediatr/actions/workflows/publish.yml)\n\nThis library ensures that [MediatR](https://github.com/jbogard/MediatR) is registered in the dependency injection container **as a singleton** and also adds several features:\n\n* [Activity-based OpenTelemetry](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/distributed-tracing-instrumentation-walkthroughs) instrumentation\n* [High-performance logging](https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator) with `Debug` log level\n* Data annotations support for request validation, similar to [ASP.NET Core model validation](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation)\n* [Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview?tabs=net) instrumentation (in a [separate NuGet package](https://www.nuget.org/packages/Workleap.Extensions.MediatR.ApplicationInsights/))\n* [CQRS](https://microservices.io/patterns/data/cqrs.html) conventions and MediatR best practices with Roslyn analyzers\n\n\n## Getting started\n\nUse the `AddMediator(params Assembly[] assemblies)` extension method on your dependency injection services (`IServiceCollection`) to automatically register all the MediatR request handlers from a given assembly.\n\n```csharp\nbuilder.Services.AddMediator(typeof(Program).Assembly /*, [more assemblies...] */);\n```\n\nIf you use [Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview?tabs=net) and want to instrument your handlers, you can install the dedicated [NuGet package](https://www.nuget.org/packages/Workleap.Extensions.MediatR.ApplicationInsights/):\n\n```csharp\nbuilder.Services.AddMediator(typeof(Program).Assembly).AddApplicationInsights();\n```\n\nThere are multiple method overloads of `AddMediator`. For instance, you can override MediatR configuration using this overload that accepts a `Action\u003cMediatRServiceConfiguration\u003e`:\n\n```csharp\nbuilder.Services.AddMediator(\n    cfg =\u003e cfg.NotificationPublisher = new TaskWhenAllPublisher(),\n    typeof(Program).Assembly);\n```\n\n\n## Example\n\n```csharp\n// CQRS naming conventions are suggested by a Roslyn analyzer, but it can be disabled\npublic sealed record SayHelloCommand([property: Required] string To) : IRequest;\n\npublic sealed class SayHelloCommandHandler : IRequestHandler\u003cSayHelloCommand\u003e\n{\n    public Task Handle(SayHelloCommand command, CancellationToken cancellationToken)\n    {\n        Console.WriteLine($\"Hello {command.To}!\");\n        return Task.CompletedTask;\n    }\n}\n\n// [...] Retrieve an instance of IMediator or ISender\nvar mediator = serviceProvider.GetRequiredService\u003cIMediator\u003e();\n\n// - We use the preferred Async-suffixed extension method to put emphasis on the asynchronous aspect of MediatR\n// - A Roslyn analyzer suggests to specify a cancellation token, which is most of the time forgotten by developers\nawait mediator.SendAsync(new SayHelloCommand(\"world\"), CancellationToken.None);\n\n// This throws RequestValidationException because 'SayHelloCommand.To' is marked as required\nawait mediator.SendAsync(new SayHelloCommand(null!), CancellationToken.None);\n```\n\n\n## Included Roslyn analyzers\n\n| Rule ID | Category | Severity | Description                                                  |\n|---------|----------|----------|--------------------------------------------------------------|\n| GMDTR01 | Naming   | Warning  | Name should end with 'Command' or 'Query'                    |\n| GMDTR02 | Naming   | Warning  | Name should end with 'CommandHandler' or 'QueryHandler'      |\n| GMDTR03 | Naming   | Warning  | Name should end with 'StreamQuery'                           |\n| GMDTR04 | Naming   | Warning  | Name should end with 'StreamQueryHandler'                    |\n| GMDTR05 | Naming   | Warning  | Name should end with 'Notification' or 'Event'               |\n| GMDTR06 | Naming   | Warning  | Name should end with 'NotificationHandler' or 'EventHandler' |\n| GMDTR07 | Design   | Warning  | Use generic method instead                                   |\n| GMDTR08 | Design   | Warning  | Provide a cancellation token                                 |\n| GMDTR09 | Design   | Warning  | Handlers should not call other handlers                      |\n| GMDTR10 | Design   | Warning  | Handlers should not be public                                |\n| GMDTR11 | Design   | Warning  | Use 'AddMediator' extension method instead of 'AddMediatR'   |\n| GMDTR12 | Design   | Warning  | Use method ending with 'Async' instead                       |\n| GMDTR13 | Naming   | Warning  | Name should end with 'Handler'                               |\n\nIn order to change the severity of one of these diagnostic rules, use a `.editorconfig` file, for instance:\n```ini\n[*.cs]\ndotnet_diagnostic.GMDTR01.severity = none\n```\nTo learn more about how to configure or suppress code analysis warnings, [read this documentation](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings). \n\n\n## Building, releasing and versioning\n\nThe project can be built by running `Build.ps1`. It uses [Microsoft.CodeAnalysis.PublicApiAnalyzers](https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) to help detect public API breaking changes. Use the built-in roslyn analyzer to ensure that public APIs are declared in `PublicAPI.Shipped.txt`, and obsolete public APIs in `PublicAPI.Unshipped.txt`.\n\nA new *preview* NuGet package is **automatically published** on any new commit on the main branch. This means that by completing a pull request, you automatically get a new NuGet package.\n\nWhen you are ready to **officially release** a stable NuGet package by following the [SemVer guidelines](https://semver.org/), simply **manually create a tag** with the format `x.y.z`. This will automatically create and publish a NuGet package for this version.\n\n\n## License\n\nCopyright © 2023, Workleap. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworkleap%2Fwl-extensions-mediatr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fworkleap%2Fwl-extensions-mediatr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworkleap%2Fwl-extensions-mediatr/lists"}