-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Manual registration of generic handler works, but automatic doesn't. #1087
Comments
usually this error occurs when you have your handlers and requests in separate assemblies. Edit: The reason for this, is that to register generic handlers, you must register the concrete handler, just like you did manually. However, to truly take advantage of the generic you would also need to manually register each and every concrete handler you intend to use (for each possible type you intend to use as a parameter to |
I tested like this: requests and handlers -> namespace TestApplication
{
public class GenericRequest<T> : IRequest<int>
{
public T Dto { get; set; }
}
public class GenericHandler<T> : IRequestHandler<GenericRequest<T>, int>
{
Task<int> IRequestHandler<GenericRequest<T>, int>.Handle(GenericRequest<T> request, CancellationToken cancellationToken)
{
return Task.FromResult(1);
}
}
} domain layer (dto) -> namespace TestDomain
{
public class MyDto
{
}
} registration (Program.cs in mvc project) using TestApplication;
using TestDomain;
builder.Services.AddMediatR(cfg =>
{
cfg.RegisterGenericHandlers = true;
cfg.RegisterServicesFromAssemblies([typeof(MyDto).Assembly, typeof(GenericHandler<>).Assembly]);
}); MVC controller -> using MediatR;
using Microsoft.AspNetCore.Mvc;
using TestApplication;
using TestDomain;
namespace TestMediatRMVC.Controllers
{
public class HomeController : Controller
{
private readonly IMediator _mediator;
public HomeController(IMediator mediator)
{
_mediator = mediator;
}
public async Task<IActionResult> Index()
{
var request = new GenericRequest<MyDto>
{
Dto = new MyDto()
};
var result = await _mediator.Send(request);
return View(result);
}
}
} Index.cshtml (view) -> @model int
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
<p>@Model</p> |
If I make the following registration:
builder.Services.AddScoped<IRequestHandler<CreateCommand<CreateClientDto, ClientEntity>, Result<ClientEntity>>, CreateHandler<CreateClientDto, ClientEntity>>();
Then at runtime everything is resolved and works. But if I leave it to automatic registration with RegisterGenericHandlers = true I have some weird error (namespaces redacted for brevity, everything is in the same assembly):
System.InvalidOperationException: No service for type 'MediatR.IRequestHandler
2[CreateCommand2[CreateClientDto,ClientEntity],Result
1[ClientEntity]]' has been registered.`Which is extremely weird, because manual registration of the very same works.
Am I missing something? I can provide more code if anyone thinks it matters, I honestly think it doesnt, like my classes implementations and such.
The text was updated successfully, but these errors were encountered: