Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use actual request type in new 'Send' method #844

Merged
merged 2 commits into from
Feb 15, 2023
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
4 changes: 2 additions & 2 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Task Send<TRequest>(TRequest request, CancellationToken cancellationToken
throw new ArgumentNullException(nameof(request));
}

var requestType = typeof(TRequest);
var requestType = request.GetType();

var handler = (RequestHandlerWrapper)_requestHandlers.GetOrAdd(requestType,
static t => (RequestHandlerBase)(Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<>).MakeGenericType(t))
Expand Down Expand Up @@ -207,4 +207,4 @@ public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TRespo

return items;
}
}
}
5 changes: 3 additions & 2 deletions test/MediatR.Tests/PublishTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ public async Task Should_resolve_handlers_given_interface()

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

INotification notification = new Ping { Message = "Ping" };
await mediator.Publish(notification);
// wrap notifications in an array, so this test won't break on a 'replace with var' refactoring
var notifications = new INotification[] { new Ping { Message = "Ping" } };
await mediator.Publish(notifications[0]);

var result = builder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
result.ShouldContain("Ping Pong");
Expand Down
25 changes: 25 additions & 0 deletions test/MediatR.Tests/SendTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ public async Task Should_resolve_main_handler_by_specific_interface()
response.Message.ShouldBe("Ping Pong");
}

[Fact]
public async Task Should_resolve_main_handler_by_given_interface()
{
var dependency = new Dependency();
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<VoidPing>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
});
cfg.ForSingletonOf<Dependency>().Use(dependency);
cfg.For<ISender>().Use<Mediator>();
});

var mediator = container.GetInstance<ISender>();

// wrap requests in an array, so this test won't break on a 'replace with var' refactoring
var requests = new IRequest[] { new VoidPing() };
await mediator.Send(requests[0]);

dependency.Called.ShouldBeTrue();
}

[Fact]
public async Task Should_raise_execption_on_null_request()
Expand Down