Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MediatR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Push.ps1 = Push.ps1
README.md = README.md
.github\workflows\release.yml = .github\workflows\release.yml
MediatR.snk = MediatR.snk
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Benchmarks", "test\MediatR.Benchmarks\MediatR.Benchmarks.csproj", "{1FA62162-F8F1-4CAD-B08E-8DCA603395AD}"
Expand Down
6 changes: 6 additions & 0 deletions src/MediatR/MediatR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<None Include="..\..\LICENSE.md" Pack="true" PackagePath=""/>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>MediatR.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010091986edd141861f402457659cb82b56cf6a0d60b3bd2e5aa4ea73d88afa929d278462d6c4c0e2ecbce21948c15514a310a82e6b2e6beaab6cb14230a03bc026609be59f938423f2490fa0033ae87a982fb4950db77d1a4635e14f7727161e93e5511de766ed8e515efd801464b7820a27fca30a32161485824e442cc5ffecfbe</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<PackageReference Include="IsExternalInit" Version="1.0.3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Microsoft.Extensions.DependencyInjection;
/// This does not scan for any <see cref="IPipelineBehavior{TRequest,TResponse}"/> instances including <see cref="RequestPreProcessorBehavior{TRequest,TResponse}"/> and <see cref="RequestPreProcessorBehavior{TRequest,TResponse}"/>.
/// To register behaviors, use the <see cref="ServiceCollectionServiceExtensions.AddTransient(IServiceCollection,Type,Type)"/> with the open generic or closed generic types.
/// </summary>
public static class ServiceCollectionExtensions
public static class MediatRServiceCollectionExtensions
{
/// <summary>
/// Registers handlers and mediator types from the specified assemblies
Expand Down Expand Up @@ -63,8 +63,13 @@ internal static void CheckLicense(this IServiceProvider serviceProvider)
{
if (LicenseChecked == false)
{
var licenseAccessor = serviceProvider.GetRequiredService<LicenseAccessor>();
var licenseValidator = serviceProvider.GetRequiredService<LicenseValidator>();
var licenseAccessor = serviceProvider.GetService<LicenseAccessor>() ?? new LicenseAccessor(
serviceProvider.GetRequiredService<MediatRServiceConfiguration>(),
serviceProvider.GetRequiredService<ILoggerFactory>()
);
var licenseValidator = serviceProvider.GetService<LicenseValidator>()
?? new LicenseValidator(serviceProvider.GetRequiredService<ILoggerFactory>());

var license = licenseAccessor.Current;
licenseValidator.Validate(license);
}
Expand Down
2 changes: 2 additions & 0 deletions src/MediatR/Registration/ServiceRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ public static void AddRequiredServices(IServiceCollection services, MediatRServi
services.TryAdd(new ServiceDescriptor(typeof(IMediator), serviceConfiguration.MediatorImplementationType, serviceConfiguration.Lifetime));
services.TryAdd(new ServiceDescriptor(typeof(ISender), sp => sp.GetRequiredService<IMediator>(), serviceConfiguration.Lifetime));
services.TryAdd(new ServiceDescriptor(typeof(IPublisher), sp => sp.GetRequiredService<IMediator>(), serviceConfiguration.Lifetime));

MediatRServiceCollectionExtensions.LicenseChecked = false;

services.TryAddSingleton(serviceConfiguration);
services.TryAddSingleton<LicenseAccessor>();
Expand Down
39 changes: 16 additions & 23 deletions test/MediatR.Tests/CreateStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace MediatR.Tests;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Shouldly;
using Lamar;
using Xunit;

public class CreateStreamTests
Expand Down Expand Up @@ -38,15 +37,13 @@ public async Task Should_resolve_main_handler()
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(CreateStreamTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IStreamRequestHandler<,>));
scanner.FromAssemblyOf<CreateStreamTests>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IStreamRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});

var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

var response = mediator.CreateStream(new Ping { Message = "Ping" });
int i = 0;
Expand All @@ -70,15 +67,13 @@ public async Task Should_resolve_main_handler_via_dynamic_dispatch()
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(CreateStreamTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IStreamRequestHandler<,>));
scanner.FromAssemblyOf<CreateStreamTests>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IStreamRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});

var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

object request = new Ping { Message = "Ping" };
var response = mediator.CreateStream(request);
Expand All @@ -103,15 +98,13 @@ public async Task Should_resolve_main_handler_by_specific_interface()
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(CreateStreamTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IStreamRequestHandler<,>));
scanner.FromAssemblyOf<CreateStreamTests>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IStreamRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<ISender>().Use<Mediator>();
cfg.AddTransient<ISender, Mediator>();
});

var mediator = container.GetInstance<ISender>();
var mediator = container.GetRequiredService<ISender>();
var response = mediator.CreateStream(new Ping { Message = "Ping" });
int i = 0;
await foreach (Pong result in response)
Expand All @@ -132,10 +125,10 @@ public void Should_raise_execption_on_null_request()
{
var container = TestContainer.Create(cfg =>
{
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});

var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

Should.Throw<ArgumentNullException>(() => mediator.CreateStream((Ping) null!));
}
Expand All @@ -145,10 +138,10 @@ public void Should_raise_execption_on_null_request_via_dynamic_dispatch()
{
var container = TestContainer.Create(cfg =>
{
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});

var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

Should.Throw<ArgumentNullException>(() => mediator.CreateStream((object) null!));
}
Expand Down
98 changes: 40 additions & 58 deletions test/MediatR.Tests/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ namespace MediatR.Tests;
using System;
using System.Threading.Tasks;
using Shouldly;
using Lamar;
using Xunit;
using Lamar.IoC;

public class ExceptionTests
{
Expand Down Expand Up @@ -73,21 +71,21 @@ public ExceptionTests()
{
var container = TestContainer.Create(cfg =>
{
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
_mediator = container.GetInstance<IMediator>();
_mediator = container.GetRequiredService<IMediator>();
}

[Fact]
public async Task Should_throw_for_send()
{
await Should.ThrowAsync<LamarMissingRegistrationException>(async () => await _mediator.Send(new Ping()));
await Should.ThrowAsync<InvalidOperationException>(async () => await _mediator.Send(new Ping()));
}

[Fact]
public async Task Should_throw_for_void_send()
{
await Should.ThrowAsync<LamarMissingRegistrationException>(async () => await _mediator.Send(new VoidPing()));
await Should.ThrowAsync<InvalidOperationException>(async () => await _mediator.Send(new VoidPing()));
}

[Fact]
Expand All @@ -108,13 +106,13 @@ public async Task Should_not_throw_for_publish()
[Fact]
public async Task Should_throw_for_async_send()
{
await Should.ThrowAsync<LamarMissingRegistrationException>(async () => await _mediator.Send(new AsyncPing()));
await Should.ThrowAsync<InvalidOperationException>(async () => await _mediator.Send(new AsyncPing()));
}

[Fact]
public async Task Should_throw_for_async_void_send()
{
await Should.ThrowAsync<LamarMissingRegistrationException>(async () => await _mediator.Send(new AsyncVoidPing()));
await Should.ThrowAsync<InvalidOperationException>(async () => await _mediator.Send(new AsyncVoidPing()));
}

[Fact]
Expand All @@ -139,14 +137,12 @@ public async Task Should_throw_argument_exception_for_send_when_request_is_null(
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(NullPing));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.FromAssemblyOf<NullPing>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

NullPing request = null!;

Expand All @@ -160,14 +156,12 @@ public async Task Should_throw_argument_exception_for_void_send_when_request_is_
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(VoidNullPing));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.FromAssemblyOf<VoidNullPing>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

VoidNullPing request = null!;

Expand All @@ -181,14 +175,12 @@ public async Task Should_throw_argument_exception_for_publish_when_request_is_nu
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(NullPinged));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.FromAssemblyOf<NullPinged>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

NullPinged notification = null!;

Expand All @@ -202,14 +194,12 @@ public async Task Should_throw_argument_exception_for_publish_when_request_is_nu
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(NullPinged));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.FromAssemblyOf<NullPinged>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

object notification = null!;

Expand All @@ -223,14 +213,12 @@ public async Task Should_throw_argument_exception_for_publish_when_request_is_no
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(NullPinged));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.FromAssemblyOf<NullPinged>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

object notification = "totally not notification";

Expand All @@ -257,15 +245,13 @@ public async Task Should_throw_exception_for_non_generic_send_when_exception_occ
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(NullPinged));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
scanner.FromAssemblyOf<NullPinged>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

object pingException = new PingException();

Expand All @@ -279,14 +265,12 @@ public async Task Should_throw_exception_for_non_request_send()
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(NullPinged));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.FromAssemblyOf<NullPinged>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

object nonRequest = new NonRequest();

Expand All @@ -306,15 +290,13 @@ public async Task Should_throw_exception_for_generic_send_when_exception_occurs(
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(NullPinged));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
scanner.FromAssemblyOf<NullPinged>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});
var mediator = container.GetInstance<IMediator>();
var mediator = container.GetRequiredService<IMediator>();

PingException pingException = new PingException();

Expand Down
14 changes: 5 additions & 9 deletions test/MediatR.Tests/GenericTypeConstraintsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace MediatR.Tests;
using System;
using System.Linq;
using Shouldly;
using Lamar;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -93,17 +92,14 @@ public GenericTypeConstraintsTests()
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(GenericTypeConstraintsTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.IncludeNamespaceContainingType<Jing>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
scanner.FromAssemblyOf<GenericTypeConstraintsTests>()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<,>))).AsImplementedInterfaces()
.AddClasses(t => t.InNamespaceOf<Ping>().AssignableTo(typeof(IRequestHandler<>))).AsImplementedInterfaces();
});
cfg.For<IMediator>().Use<Mediator>();
cfg.AddTransient<IMediator, Mediator>();
});

_mediator = container.GetInstance<IMediator>();
_mediator = container.GetRequiredService<IMediator>();
}

[Fact]
Expand Down
Loading
Loading