MediatR.Extensions.Microsoft.AspNetCore
6.0.0
dotnet add package MediatR.Extensions.Microsoft.AspNetCore --version 6.0.0
NuGet\Install-Package MediatR.Extensions.Microsoft.AspNetCore -Version 6.0.0
<PackageReference Include="MediatR.Extensions.Microsoft.AspNetCore" Version="6.0.0" />
paket add MediatR.Extensions.Microsoft.AspNetCore --version 6.0.0
#r "nuget: MediatR.Extensions.Microsoft.AspNetCore, 6.0.0"
// Install MediatR.Extensions.Microsoft.AspNetCore as a Cake Addin #addin nuget:?package=MediatR.Extensions.Microsoft.AspNetCore&version=6.0.0 // Install MediatR.Extensions.Microsoft.AspNetCore as a Cake Tool #tool nuget:?package=MediatR.Extensions.Microsoft.AspNetCore&version=6.0.0
MediatR.Extensions.Microsoft.AspNetCore
Project summary
Some useful extensions to make the integration of MediatR into ASP.NET Core Application easier. It currently only adds one new feature to the integration of MediatR into ASP.NET Core: Automatic wire-up of the HttpContext.RequestAborted CancellationToken into the the handlers. I might add a few more features in the future as well, as I already have some ideas.
As this library is in an early stage, there might also be some breaking changes in the future.
Integration into ASP.NET Core
The extension is registered in an ASP.NET Core Application like this:
services
.AddControllers()
.AddMediatRUsingRequestAbortedCancellationToken(config => config.AsScoped(), typeof(Startup).Assembly)
Important: You will not have to register MediatR using AddMediatR now anymore. So remove that call from ConfigureServices and move any configuration you might have to the overloads of AddMediatRUsingRequestAbortedCancellationToken.
Some more explanation
This library removes the need to explicitly pass the CancellationToken in Controllers on every call manually, which until now had to be like this:
mediator.Send(request, HttpContext.RequestAborted);
mediator.Publish(notification, HttpContext.RequestAborted);
Adding this was easily forgotten. Using this library, calls to MediatR can simply be used like this:
mediator.Send(request);
mediator.Publish(notification);
And the library will pass the HttpContext.RequestAborted CancellationToken to the handlers (in case an ambient HttpContext does exist).
As of version 3.0.0, a behavior change was implemented, making the library usable in more contexts: If you pass your own token, behavior depends on the existence of a usable HttpContext.RequestAborted token. CancellationToken then
// Passing own token
mediator.Send(request, myCancellationToken);
In case the RequestAborted token is accessible, it is merged with your own token using a linked source. In case no RequestAborted does exist for whatever reason, only the passed token is used as fallback.
The integration was designed to be as simple as possible, and tries to keep all the existing MediatR extension methods for registering. To avoid confusion with the regular MediatR registration and to explicitly make it clear that this library depends on the ASP.NET Core API (because it makes use of the IHttpContextAccessor), the registration of this library is based of the IMvcBuilder interface returned by the AddControllers methods of the IServiceCollection.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
-
net7.0
- MediatR.Extensions.Microsoft.DependencyInjection (>= 11.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.