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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MediatR.Extensions.Microsoft.AspNetCore" Version="6.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MediatR.Extensions.Microsoft.AspNetCore --version 6.0.0
#r "nuget: MediatR.Extensions.Microsoft.AspNetCore, 6.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

NuGet

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.0 15,366 11/28/2022
5.0.0 30,500 4/5/2022
4.0.0 10,755 12/5/2021
3.0.1 3,345 8/16/2021
3.0.0 1,070 6/1/2021
2.0.0 975 11/27/2020
1.0.0 2,087 2/23/2020