< Summary

Information
Class: Microsoft.Extensions.DependencyInjection.DependencyInjectionExtensions
Assembly: Elsa.Mediator
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs
Line coverage
62%
Covered lines: 41
Uncovered lines: 25
Coverable lines: 66
Total lines: 241
Line coverage: 62.1%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs

#LineLine coverage
 1using System.Reflection;
 2using System.Threading.Channels;
 3using Elsa.Mediator.Channels;
 4using Elsa.Mediator.Contracts;
 5using Elsa.Mediator.HostedServices;
 6using Elsa.Mediator.Middleware.Command;
 7using Elsa.Mediator.Middleware.Command.Contracts;
 8using Elsa.Mediator.Middleware.Notification;
 9using Elsa.Mediator.Middleware.Notification.Contracts;
 10using Elsa.Mediator.Middleware.Request;
 11using Elsa.Mediator.Middleware.Request.Contracts;
 12using Elsa.Mediator.Models;
 13using Elsa.Mediator.Options;
 14using Elsa.Mediator.Services;
 15using JetBrains.Annotations;
 16using Microsoft.Extensions.Logging;
 17
 18// ReSharper disable once CheckNamespace
 19namespace Microsoft.Extensions.DependencyInjection;
 20
 21/// <summary>
 22/// Adds mediator services to the <see cref="IServiceCollection"/>.
 23/// </summary>
 24[PublicAPI]
 25public static class DependencyInjectionExtensions
 26{
 27    /// <summary>
 28    /// Adds mediator services to the <see cref="IServiceCollection"/>.
 29    /// </summary>
 30    public static IServiceCollection AddMediator(this IServiceCollection services, Action<MediatorOptions>? configure = 
 31    {
 1632        services.Configure(configure ?? (_ => { }));
 33
 834        return services
 835                .AddScoped<IMediator, DefaultMediator>()
 036                .AddScoped<IRequestSender>(sp => sp.GetRequiredService<IMediator>())
 40537                .AddScoped<ICommandSender>(sp => sp.GetRequiredService<IMediator>())
 41438                .AddScoped<INotificationSender>(sp => sp.GetRequiredService<IMediator>())
 839                .AddSingleton<IRequestPipeline, RequestPipeline>()
 840                .AddSingleton<ICommandPipeline, CommandPipeline>()
 841                .AddSingleton<INotificationPipeline, NotificationPipeline>()
 842            ;
 43    }
 44
 45    /// <summary>
 46    /// Adds mediator hosted services to the <see cref="IServiceCollection"/>.
 47    /// </summary>
 48    /// <param name="services"></param>
 49    /// <returns></returns>
 50    public static IServiceCollection AddMediatorHostedServices(this IServiceCollection services)
 51    {
 352        return services
 353            .AddMediatorBackgroundChannels()
 354            .AddHostedService<JobRunnerHostedService>()
 355            .AddHostedService<BackgroundCommandSenderHostedService>()
 356            .AddHostedService<BackgroundEventPublisherHostedService>();
 57    }
 58
 59    /// <summary>
 60    /// Adds mediator background processing channels to the <see cref="IServiceCollection"/>.
 61    /// </summary>
 62    public static IServiceCollection AddMediatorBackgroundChannels(this IServiceCollection services)
 63    {
 364        return services
 365            .AddSingleton<INotificationsChannel, NotificationsChannel>()
 366            .AddSingleton<ICommandsChannel, CommandsChannel>()
 367            .AddSingleton<IJobsChannel, JobsChannel>()
 368            .AddSingleton<IJobQueue, JobQueue>()
 369            .AddSingleton<BackgroundCommandProcessor>()
 370            .AddSingleton<BackgroundNotificationProcessor>()
 371            .AddSingleton<BackgroundJobProcessor>()
 372            ;
 73    }
 74
 75    /// <summary>
 76    /// Adds a <see cref="Channel{T}"/> to the <see cref="IServiceCollection"/> and a hosted service that continuously r
 77    /// </summary>
 78    public static IServiceCollection AddMessageChannel<T>(this IServiceCollection services, int workerCount = 1) where T
 79    {
 080        services.AddSingleton<Channel<T>>(_ => CreateChannel<T>());
 81
 082        services.AddHostedService(sp =>
 083        {
 084            var channel = sp.GetRequiredService<Channel<T>>();
 085            var consumers = sp.GetServices<IConsumer<T>>();
 086            var logger = sp.GetRequiredService<ILogger<MessageProcessorHostedService<T>>>();
 087            return new MessageProcessorHostedService<T>(workerCount, channel, consumers, logger);
 088        });
 089        return services;
 90    }
 91
 92    /// <summary>
 93    /// Adds a channel consumer.
 94    /// </summary>
 95    public static IServiceCollection AddMessageConsumer<T, TConsumer>(this IServiceCollection services) where TConsumer 
 96    {
 097        return services.AddScoped<IConsumer<T>, TConsumer>();
 98    }
 99
 100    /// <summary>
 101    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 102    /// </summary>
 103    public static IServiceCollection AddCommandHandler<THandler>(this IServiceCollection services) where THandler : clas
 14104        services.AddScoped<ICommandHandler, THandler>();
 105
 106    /// <summary>
 107    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 108    /// </summary>
 109    public static IServiceCollection AddCommandHandler<THandler, TCommand>(this IServiceCollection services)
 110        where THandler : class, ICommandHandler<TCommand> where TCommand : ICommand<Unit> =>
 0111        services.AddCommandHandler<THandler, TCommand, Unit>();
 112
 113    /// <summary>
 114    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 115    /// </summary>
 116    public static IServiceCollection AddCommandHandler<THandler, TCommand, TResult>(this IServiceCollection services)
 117        where THandler : class, ICommandHandler<TCommand, TResult>
 118        where TCommand : ICommand<TResult>
 119    {
 0120        return services.AddScoped<ICommandHandler, THandler>();
 121    }
 122
 123    /// <summary>
 124    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 125    /// </summary>
 126    public static IServiceCollection AddNotificationHandler<THandler>(this IServiceCollection services)
 127        where THandler : class, INotificationHandler
 128    {
 75129        return services.AddScoped<INotificationHandler, THandler>();
 130    }
 131
 132    /// <summary>
 133    /// Registers a <see cref="INotificationHandler{T}"/> with the service container.
 134    /// </summary>
 135    public static IServiceCollection AddNotificationHandler<THandler, TNotification>(this IServiceCollection services)
 136        where THandler : class, INotificationHandler<TNotification>
 137        where TNotification : INotification =>
 0138        services.AddScoped<INotificationHandler, THandler>();
 139
 140    /// <summary>
 141    /// Registers a <see cref="INotificationHandler{T}"/> with the service container.
 142    /// </summary>
 143    public static IServiceCollection AddNotificationHandler<THandler, TNotification>(this IServiceCollection services, F
 144        where THandler : class, INotificationHandler<TNotification>
 145        where TNotification : INotification =>
 0146        services.AddScoped<INotificationHandler, THandler>(factory);
 147
 148    /// <summary>
 149    /// Registers a <see cref="IRequestHandler{TRequest,TResponse}"/> with the service container.
 150    /// </summary>
 151    public static IServiceCollection AddRequestHandler<THandler, TRequest, TResponse>(this IServiceCollection services)
 152        where THandler : class, IRequestHandler<TRequest, TResponse>
 153        where TRequest : IRequest<TResponse>?
 154    {
 0155        return services.AddScoped<IRequestHandler, THandler>();
 156    }
 157
 158    /// <summary>
 159    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 160    /// </summary>
 12161    public static IServiceCollection AddHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHandlersF
 162
 163    /// <summary>
 164    /// Registers all handlers from the assembly of the specified marker type with the service container.
 165    /// </summary>
 12166    public static IServiceCollection AddHandlersFrom(this IServiceCollection services, Type markerType) => services.AddH
 167
 168    /// <summary>
 169    /// Registers all handlers from the specified assembly with the service container.
 170    /// </summary>
 171    public static IServiceCollection AddHandlersFrom(this IServiceCollection services, Assembly assembly)
 172    {
 12173        return services
 12174            .AddNotificationHandlersFrom(assembly)
 12175            .AddRequestHandlersFrom(assembly)
 12176            .AddCommandHandlersFrom(assembly);
 177    }
 178
 179    /// <summary>
 180    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 181    /// </summary>
 18182    public static IServiceCollection AddNotificationHandlersFrom<TMarker>(this IServiceCollection services) => services.
 183
 184    /// <summary>
 185    /// Registers all handlers from the assembly of the specified marker type with the service container.
 186    /// </summary>
 0187    public static IServiceCollection AddNotificationHandlersFrom(this IServiceCollection services, Type markerType) => s
 188
 189    /// <summary>
 190    /// Registers all handlers from the specified assembly with the service container.
 191    /// </summary>
 12192    public static IServiceCollection AddNotificationHandlersFrom(this IServiceCollection services, Assembly assembly) =>
 193
 194    /// <summary>
 195    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 196    /// </summary>
 0197    public static IServiceCollection AddRequestHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHa
 198
 199    /// <summary>
 200    /// Registers all handlers from the assembly of the specified marker type with the service container.
 201    /// </summary>
 0202    public static IServiceCollection AddRequestHandlersFrom(this IServiceCollection services, Type markerType) => servic
 203
 204    /// <summary>
 205    /// Registers all handlers from the specified assembly with the service container.
 206    /// </summary>
 12207    public static IServiceCollection AddRequestHandlersFrom(this IServiceCollection services, Assembly assembly) => serv
 208
 209    /// <summary>
 210    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 211    /// </summary>
 0212    public static IServiceCollection AddCommandHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHa
 213
 214    /// <summary>
 215    /// Registers all handlers from the assembly of the specified marker type with the service container.
 216    /// </summary>
 0217    public static IServiceCollection AddCommandHandlersFrom(this IServiceCollection services, Type markerType) => servic
 218
 219    /// <summary>
 220    /// Registers all handlers from the specified assembly with the service container.
 221    /// </summary>
 12222    public static IServiceCollection AddCommandHandlersFrom(this IServiceCollection services, Assembly assembly) => serv
 223
 0224    private static IServiceCollection AddHandlersFromInternal<TService, TMarker>(this IServiceCollection services) => se
 18225    private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Type assemblyM
 226
 227    private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Assembly assem
 228    {
 54229        var serviceType = typeof(TService);
 13518230        var types = assembly.DefinedTypes.Where(x => serviceType.IsAssignableFrom(x));
 231
 246232        foreach (var type in types)
 69233            services.AddScoped(serviceType, type);
 234
 54235        return services;
 236    }
 237
 0238    private static Channel<T> CreateChannel<T>() => Channel.CreateUnbounded<T>(new UnboundedChannelOptions());
 0239    private static ChannelReader<T> CreateChannelReader<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi
 0240    private static ChannelWriter<T> CreateChannelWriter<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi
 241}

Methods/Properties

AddMediator(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action`1<Elsa.Mediator.Options.MediatorOptions>)
AddMediatorHostedServices(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddMediatorBackgroundChannels(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddMessageChannel(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Int32)
AddMessageConsumer(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddCommandHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddNotificationHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddNotificationHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Func`2<System.IServiceProvider,THandler>)
AddRequestHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddNotificationHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddNotificationHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddNotificationHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddRequestHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddRequestHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddRequestHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddCommandHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddCommandHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddCommandHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddHandlersFromInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddHandlersFromInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddHandlersFromInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
CreateChannel()
CreateChannelReader(System.IServiceProvider)
CreateChannelWriter(System.IServiceProvider)