MediatR 3.0.1 Cannot get IAsyncRequestHandler working
See original GitHub issueI am getting the following error message when executing IRequest with IAsyncRequestHandler.
System.InvalidOperationException: 'No service for type 'MediatR.IRequestHandler`2[TestProject.Domain.Requests.Users.CreateUserRequest,TestProject.Domain.Requests.Users.CreateUserResponse]' has been registered.'
This is how i register it in the startup class
// Add framework services.
services.AddMvc();
services.AddMediatR(typeof(CreateUserRequest).GetTypeInfo().Assembly);
CreateUserRequest and Response class
public class CreateUserRequest : IRequest<CreateUserResponse>
{
public string EmailAddress { get; set; }
public int OrganisationId { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CreateUserResponse
{
public int UserId { get; set; }
public string EmailAddress { get; set; }
}
Request handler class
public class CreateUserRequestHandler : IAsyncRequestHandler<CreateUserRequest, CreateUserResponse>
{
private readonly UserManager<User> _userManager;
public CreateUserRequestHandler()
{
}
public async Task<CreateUserResponse> Handle(CreateUserRequest request)
{
//create the user and assign it to the organisation
var user = new User
{
Email = request.EmailAddress,
OrganisationUsers = new List<OrganisationUser> { new OrganisationUser { OrganisationId = request.OrganisationId } }
};
//create new user with password.
await _userManager.CreateAsync(user, request.Password);
//create response.
var response = new CreateUserResponse{UserId = user.Id, EmailAddress = user.Email};
return response;
}
Controller class
public class UserController : Controller
{
private readonly IMediator _mediator;
public UserController(IMediator mediator)
{
_mediator = mediator;
}
[HttpPost]
public async Task<CreateUserResponse> Post(CreateUserRequest request)
{
return await _mediator.Send(request);
}
}
the error occurs inside the controller class it does not hit the async request handler.
Is there anything wrong with the DI registration? I have looked at the examples but could not find anything specific to aspnet core.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
asp.net core - MediatR 3.0.1 possible bug? Cannot get ...
I am getting the following error message when executing IRequest with IAsyncRequestHandler. System.InvalidOperationException: 'No service ...
Read more >Unanswered 'mediatr' Questions
I am getting the following error message when executing IRequest with IAsyncRequestHandler. System.InvalidOperationException: 'No service for type 'MediatR.
Read more >CQRS with Mediatr and ASP.NET Core - Steve Gordon
An introduction to implementing the CQRS pattern in ASP.NET Core using the Mediatr library.
Read more >MediatR Behaviors
I am still getting the cannot resolve IRequestHandler`2 when using the IAsyncRequestHandler but it works fine for IRequestHandler. Any ideas?
Read more >CQRS and MediatR in ASP.NET Core
How to use the MediatR NuGet library in ASP.NET Core, and working with the CQRS and Mediator architectural patterns.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can use this code in the startup.cs For example, there are 5 projects in your solution and you can use all of them
My issue was that I had the query request and the response model in the Domain project and the query hander in the BusinessLogic project.