private static void AddRequiredServices(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration) { services.AddScoped <ServiceFactory>(p => p.GetService); services.AddScoped(typeof(IPipelineBehavior <,>), typeof(RequestPreProcessorBehavior <,>)); services.AddScoped(typeof(IPipelineBehavior <,>), typeof(RequestPostProcessorBehavior <,>)); services.Add(new ServiceDescriptor(typeof(IMediator), serviceConfiguration.MediatorImplementationType, serviceConfiguration.Lifetime)); }
/// <summary> /// Registers handlers and mediator types from the assemblies that contain the specified types /// </summary> /// <param name="services"></param> /// <param name="handlerAssemblyMarkerTypes"></param> /// <param name="configuration">The action used to configure the options</param> /// <returns>Service collection</returns> public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable <Type> handlerAssemblyMarkerTypes, Action <MediatRServiceConfiguration> configuration) { var serviceConfig = new MediatRServiceConfiguration(); configuration?.Invoke(serviceConfig); AddRequiredServices(services, serviceConfig); AddMediatRClasses(services, handlerAssemblyMarkerTypes.Select(t => t.GetTypeInfo().Assembly)); return(services); }
public static IServiceCollection AddMediatRCore(this IServiceCollection services, Action <MediatRServiceConfiguration> configuration = null) { var serviceConfig = new MediatRServiceConfiguration(); configuration?.Invoke(serviceConfig); ServiceRegistrar.AddRequiredServices(services, serviceConfig); return(services); }
/// <summary> /// Registers handlers and mediator types from the specified assemblies /// </summary> /// <param name="services">Service collection</param> /// <param name="assemblies">Assemblies to scan</param> /// <param name="configuration">The action used to configure the options</param> /// <returns>Service collection</returns> public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable <Assembly> assemblies, Action <MediatRServiceConfiguration> configuration) { var serviceConfig = new MediatRServiceConfiguration(); configuration?.Invoke(serviceConfig); AddRequiredServices(services, serviceConfig); AddMediatRClasses(services, assemblies); return(services); }
/// <summary> /// Registers handlers and mediator types from the specified assemblies /// </summary> /// <param name="services">Service collection</param> /// <param name="assemblies">Assemblies to scan</param> /// <param name="configuration">The action used to configure the options</param> /// <returns>Service collection</returns> public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable <Assembly> assemblies, Action <MediatRServiceConfiguration> configuration) { if (!assemblies.Any()) { throw new ArgumentException("No assemblies found to scan. Supply at least one assembly to scan for handlers."); } var serviceConfig = new MediatRServiceConfiguration(); configuration?.Invoke(serviceConfig); ServiceRegistrar.AddRequiredServices(services, serviceConfig); ServiceRegistrar.AddMediatRClasses(services, assemblies); return(services); }
private static void AddRequiredServices(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration) { services.AddScoped <ServiceFactory>(p => type => { try { return(p.GetService(type)); } catch (ArgumentException) { // Let's assume it's a constrained generic type if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { var serviceType = type.GenericTypeArguments.Single(); var serviceTypes = new List <Type>(); foreach (var service in services.ToList()) { if (serviceType.IsConstructedGenericType && serviceType.GetGenericTypeDefinition() == service.ServiceType) { try { var closedImplType = service.ImplementationType.MakeGenericType(serviceType.GenericTypeArguments); serviceTypes.Add(closedImplType); } catch (ArgumentException) { } } } services.Replace(new ServiceDescriptor(type, sp => { return(serviceTypes.Select(sp.GetService).ToArray()); }, ServiceLifetime.Transient)); var resolved = Array.CreateInstance(serviceType, serviceTypes.Count); Array.Copy(serviceTypes.Select(p.GetService).ToArray(), resolved, serviceTypes.Count); return(resolved); } throw; } }); services.Add(new ServiceDescriptor(typeof(IMediator), serviceConfiguration.MediatorImplementationType, serviceConfiguration.Lifetime)); }