diff --git a/src/MediatR.Extensions.Microsoft.DependencyInjection/Registration/ServiceRegistrar.cs b/src/MediatR.Extensions.Microsoft.DependencyInjection/Registration/ServiceRegistrar.cs index 6c9b260..ae9d048 100644 --- a/src/MediatR.Extensions.Microsoft.DependencyInjection/Registration/ServiceRegistrar.cs +++ b/src/MediatR.Extensions.Microsoft.DependencyInjection/Registration/ServiceRegistrar.cs @@ -10,7 +10,11 @@ namespace MediatR.Registration { public static class ServiceRegistrar { - public static void AddMediatRClasses(IServiceCollection services, IEnumerable assembliesToScan) + + public static void AddMediatRClasses(this IServiceCollection services, params Assembly[] assembliesToScan) + => services.AddMediatRClasses((IEnumerable) assembliesToScan); + + public static void AddMediatRClasses(this IServiceCollection services, IEnumerable assembliesToScan) { assembliesToScan = (assembliesToScan as Assembly[] ?? assembliesToScan).Distinct().ToArray(); diff --git a/src/MediatR.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs b/src/MediatR.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs index 41c3133..bc60d89 100644 --- a/src/MediatR.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/MediatR.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs @@ -19,6 +19,15 @@ namespace MediatR /// public static class ServiceCollectionExtensions { + /// + /// Registers mediator types only. Handlers must be registered separately, possibly with . + /// + /// Service collection + /// Service collection + public static IServiceCollection AddMediatR(this IServiceCollection services) + => services.AddMediatR(new Assembly[0], configuration: null); + + /// /// Registers handlers and mediator types from the specified assemblies /// @@ -47,10 +56,6 @@ public static IServiceCollection AddMediatR(this IServiceCollection services, Ac /// Service collection public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable assemblies, Action 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); diff --git a/test/MediatR.Extensions.Microsoft.DependencyInjection.Tests/AssemblyResolutionTests.cs b/test/MediatR.Extensions.Microsoft.DependencyInjection.Tests/AssemblyResolutionTests.cs index b5a352d..bd69511 100644 --- a/test/MediatR.Extensions.Microsoft.DependencyInjection.Tests/AssemblyResolutionTests.cs +++ b/test/MediatR.Extensions.Microsoft.DependencyInjection.Tests/AssemblyResolutionTests.cs @@ -1,8 +1,10 @@ -using Microsoft.Extensions.DependencyInjection; +using MediatR.Registration; +using Microsoft.Extensions.DependencyInjection; namespace MediatR.Extensions.Microsoft.DependencyInjection.Tests { using System; + using System.Collections.Generic; using System.Linq; using System.Reflection; using Shouldly; @@ -45,13 +47,33 @@ public void ShouldResolveNotificationHandlers() } [Fact] - public void ShouldRequireAtLeastOneAssembly() + public void ShouldNotRequireAtLeastOneAssembly() { var services = new ServiceCollection(); Action registration = () => services.AddMediatR(new Type[0]); - registration.ShouldThrow(); + registration.ShouldNotThrow(); + } + + [Fact] + public void ShouldResolveHandlerWithParamsRegistration() + { + var services = new ServiceCollection(); + services.AddMediatR(); + services.AddMediatRClasses(typeof(Ping).GetTypeInfo().Assembly); + + _provider.GetService>().ShouldNotBeNull(); + } + + [Fact] + public void ShouldResolveHandlerWithEnumerableRegistration() + { + var services = new ServiceCollection(); + services.AddMediatR(); + services.AddMediatRClasses(new List() { typeof(Ping).GetTypeInfo().Assembly }); + + _provider.GetService>().ShouldNotBeNull(); } } } \ No newline at end of file