question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MediatR 3.0.1 Cannot get IAsyncRequestHandler working

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ergincelikcommented, Jan 16, 2022

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

       public void ConfigureServices(IServiceCollection services)
      {
             Assembly.GetEntryAssembly().GetReferencedAssemblies()
                                                            .Where(c => c.FullName.StartsWith("App.")).ToList()
                                                           .ForEach(c => Assembly.Load(c.FullName));
      }
0reactions
MisiMcommented, Jan 15, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found