Lanre Afeez
Lanre Afeez

Reputation: 31

"The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments

Am trying to use the mediator pattern in my api but am getting some errors I don't understand. Here is the error "The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments.

[ApiController]
    [Route("api/v{version:apiVersion}/[controller]")]
    public abstract class BaseApiController : ControllerBase
    {
        private IMediator _mediator;

        protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();



    }

Upvotes: 0

Views: 667

Answers (1)

Adding using Microsoft.Extensions.DependencyInjection manually helped me.

Also you might want to consider using .GetRequiredService<>() which throws an Exception if the service is not registered, which might help better debug.

Also, although you can still use IMediator, you might want to consider using ISender to be more strict about it.

Upvotes: 2

Related Questions