-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore release: Updating dependencies to .net 6
- Loading branch information
Showing
6 changed files
with
413 additions
and
426 deletions.
There are no files selected for viewing
144 changes: 70 additions & 74 deletions
144
src/MediatR.Extensions.Microsoft.AspNetCore/AspNetCoreMediatRServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,83 @@ | ||
namespace MediatR.Extensions.Microsoft.AspNetCore | ||
| ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MediatR.Extensions.Microsoft.AspNetCore.Mediator; | ||
|
||
namespace MediatR.Extensions.Microsoft.AspNetCore; | ||
/// <summary> | ||
/// Entry point for registration extension methods that work on top of IMvcBuilder | ||
/// </summary> | ||
public static class AspNetCoreMediatRServiceCollectionExtensions | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using global::Microsoft.AspNetCore.Http; | ||
using global::Microsoft.Extensions.DependencyInjection; | ||
using Mediator; | ||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the provided assemblies for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="assemblies">Assemblies to scan for requests and handlers</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, params Assembly[] assemblies) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(null, assemblies); | ||
|
||
/// <summary> | ||
/// Entry point for registration extension methods that work on top of IMvcBuilder | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the provided assemblies for handlers and requests | ||
/// </summary> | ||
public static class AspNetCoreMediatRServiceCollectionExtensions | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="configuration">The action used to optionally configure the MediatR options</param> | ||
/// <param name="assemblies">Assemblies to scan for requests and handlers</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, Action<MediatRServiceConfiguration>? configuration, params Assembly[] assemblies) | ||
{ | ||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the provided assemblies for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="assemblies">Assemblies to scan for requests and handlers</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, params Assembly[] assemblies) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(null, assemblies); | ||
var serviceConfig = new MediatRServiceConfiguration(); | ||
configuration?.Invoke(serviceConfig); | ||
|
||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the provided assemblies for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="configuration">The action used to optionally configure the MediatR options</param> | ||
/// <param name="assemblies">Assemblies to scan for requests and handlers</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, Action<MediatRServiceConfiguration>? configuration, params Assembly[] assemblies) | ||
{ | ||
var serviceConfig = new MediatRServiceConfiguration(); | ||
configuration?.Invoke(serviceConfig); | ||
mvcBuilder.Services.AddMediatR(assemblies, configuration); | ||
ConfigureMediatorDecorator(mvcBuilder.Services, serviceConfig); | ||
|
||
mvcBuilder.Services.AddMediatR(assemblies, configuration); | ||
ConfigureMediatorDecorator(mvcBuilder.Services, serviceConfig); | ||
|
||
return mvcBuilder; | ||
} | ||
return mvcBuilder; | ||
} | ||
|
||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the assemblies of the provided types for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="handlerAssemblyMarkerTypes">Assembly handler marker types</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, params Type[] handlerAssemblyMarkerTypes) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(handlerAssemblyMarkerTypes, null); | ||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the assemblies of the provided types for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="handlerAssemblyMarkerTypes">Assembly handler marker types</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, params Type[] handlerAssemblyMarkerTypes) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(handlerAssemblyMarkerTypes, null); | ||
|
||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the assemblies of the provided types for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="configuration">The action used to optionally configure the MediatR options</param> | ||
/// <param name="handlerAssemblyMarkerTypes">Assembly handler marker types</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, Action<MediatRServiceConfiguration> configuration, params Type[] handlerAssemblyMarkerTypes) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(handlerAssemblyMarkerTypes, configuration); | ||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the assemblies of the provided types for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">Mvc Builder</param> | ||
/// <param name="configuration">The action used to optionally configure the MediatR options</param> | ||
/// <param name="handlerAssemblyMarkerTypes">Assembly handler marker types</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken(this IMvcBuilder mvcBuilder, Action<MediatRServiceConfiguration> configuration, params Type[] handlerAssemblyMarkerTypes) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(handlerAssemblyMarkerTypes, configuration); | ||
|
||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the assemblies of the provided types for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">>Mvc Builder</param> | ||
/// <param name="handlerAssemblyMarkerTypes">Assembly handler marker types</param> | ||
/// <param name="configuration">The action used to optionally configure the MediatR options</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken( | ||
this IMvcBuilder mvcBuilder, IEnumerable<Type> handlerAssemblyMarkerTypes, Action<MediatRServiceConfiguration>? configuration) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(configuration, handlerAssemblyMarkerTypes.Select(t => t.GetTypeInfo().Assembly).ToArray()); | ||
/// <summary> | ||
/// Registers MediatR which is configured to use the HttpContext.RequestAborted CancellationToken in ASP.NET Core Environments, and scans the assemblies of the provided types for handlers and requests | ||
/// </summary> | ||
/// <param name="mvcBuilder">>Mvc Builder</param> | ||
/// <param name="handlerAssemblyMarkerTypes">Assembly handler marker types</param> | ||
/// <param name="configuration">The action used to optionally configure the MediatR options</param> | ||
/// <returns></returns> | ||
public static IMvcBuilder AddMediatRUsingRequestAbortedCancellationToken( | ||
this IMvcBuilder mvcBuilder, IEnumerable<Type> handlerAssemblyMarkerTypes, Action<MediatRServiceConfiguration>? configuration) | ||
=> mvcBuilder.AddMediatRUsingRequestAbortedCancellationToken(configuration, handlerAssemblyMarkerTypes.Select(t => t.GetTypeInfo().Assembly).ToArray()); | ||
|
||
private static void ConfigureMediatorDecorator(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration) | ||
{ | ||
services.Add(new ServiceDescriptor(serviceConfiguration.MediatorImplementationType, serviceConfiguration.MediatorImplementationType, serviceConfiguration.Lifetime)); | ||
services.AddScoped<IMediator, RequestAbortedCancellationTokenMediatorDecorator>( | ||
provider => | ||
{ | ||
var mediator = (IMediator?) provider.GetService(serviceConfiguration.MediatorImplementationType) | ||
?? throw new InvalidOperationException($"Could not resolve Mediator implementation {serviceConfiguration.MediatorImplementationType} from service provider"); | ||
var httpContextAccessor = provider.GetService<IHttpContextAccessor>() | ||
?? throw new InvalidOperationException("Could not resolve HttpContextAccessor from service provider."); | ||
return new RequestAbortedCancellationTokenMediatorDecorator(mediator, httpContextAccessor); | ||
}); | ||
} | ||
private static void ConfigureMediatorDecorator(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration) | ||
{ | ||
services.Add(new ServiceDescriptor(serviceConfiguration.MediatorImplementationType, serviceConfiguration.MediatorImplementationType, serviceConfiguration.Lifetime)); | ||
services.AddScoped<IMediator, RequestAbortedCancellationTokenMediatorDecorator>( | ||
provider => | ||
{ | ||
var mediator = (IMediator?)provider.GetService(serviceConfiguration.MediatorImplementationType) | ||
?? throw new InvalidOperationException($"Could not resolve Mediator implementation {serviceConfiguration.MediatorImplementationType} from service provider"); | ||
var httpContextAccessor = provider.GetService<IHttpContextAccessor>() | ||
?? throw new InvalidOperationException("Could not resolve HttpContextAccessor from service provider."); | ||
return new RequestAbortedCancellationTokenMediatorDecorator(mediator, httpContextAccessor); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.