-
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
Issue: "No service for type 'MediatR.IRequestHandler'" #1092
Comments
I'm guessing that your handlers are in a different assembly than your requests. If that is the case then you just need to add both assemblies in the AddMediatR configuration options. Also, the types you are passing as generic type parameters must also be available during the registration of mediatR so you'll also need to include those assemblies as well. Also the internal access modifiers on your handler and requests might also be causing some issues. |
namespace Assembly1{
public class MyDto{
}
}
namespace Assembly2{
public class GenericBasicTypeNotification<T> : BaseNotification {
// ...
}
}
namespace Assembly3{
public class GenericBaseTypeNoResponseHandler<T> : HandlerBaseProperties, RequestHandler<GenericBasicTypeNotification<T>>
{
public async Task Handle(GenericBasicTypeNotification<T> request, CancellationToken cancellationToken) {
// ...
}
}
}
//AddMediatR method
services.AddMediatR(cfg => {
cfg.RegisterGenericHandlers = true;
cfg.RegisterServicesFromAssemblies([typeof(MyDto).Assmebly, typeof(GenericBasicTypeNotification<>).Assembly, typeof(GenericBaseTypeNoResponseHandler<>).Assembly]);
});
//remember all assemblies need to be included so that they are available to scan during AddMediatR registration. |
Ok so this specific issue is also caused by the fact that the auto registration only scans the assemblies you provide. Your assemblies do not define the primitive types so you'll need to add them. builder.Services.AddMediatR(cfg =>
{
//add this to disable the constraint on number of types that can close.
cfg.MaxTypesClosing = 0;
//enable generic handlers
cfg.RegisterGenericHandlers = true;
//add typeof(int).Assembly to include primitive types in assembly scanning.
cfg.RegisterServicesFromAssemblies([typeof(Startup).Assembly, typeof(int).Assembly]);
}); Request / Handler Definition -> using MediatR;
namespace TestApplication
{
public class GenericRequest<T> : IRequest<T>
{
public T Payload { get; set; }
}
public class GenericHandler<T> : IRequestHandler<GenericRequest<T>, T>
{
public Task<T> Handle(GenericRequest<T> request, CancellationToken cancellationToken)
{
return Task.FromResult(request.Payload);
}
}
} MVC controller -> using MediatR;
using Microsoft.AspNetCore.Mvc;
using TestApplication;
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<string>
{
Payload = "Hello World!"
};
var result = await _mediator.Send(request);
ViewBag.Result = result;
return View();
}
}
} Razor view -> @{
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>@ViewBag.Result</p> This should fix your issue. Cheers! |
Thanks for your comments. I will test it. Last week I have been very busy. |
Environment
Issue description
I want to use generic handlers with notifications. Due to I was not able to do it, I have changed to generic requests.
And now I'm getting the below error message.
Issue error message
Source code
Handlers
Configure services
Configure
methodExample of use and it generates the error
other links read and tests performed
The text was updated successfully, but these errors were encountered: