Mediatr: Unable to resolve service for type 'MediatR.Mediator&#x27

Created on 26 May 2019  路  3Comments  路  Source: jbogard/MediatR

Hello Jimmy,

I am getting an unhandled exception occurred while processing the request "InvalidOperationException: Unable to resolve service for type 'MediatR.Mediator' while attempting to activate 'NaijaEvent.Service.Controllers.EventController'"

See my startup class
```c#
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NaijaEvent.Application.NEvents.Command.CreateEvent;
using NaijaEvent.Application.NEvents.Query;
using NaijaEvent.Persistance;
using MediatR;
using System.Reflection;
using Swashbuckle.AspNetCore.Swagger;
using AutoMapper;
using NaijaEvent.Application.NEvents;

namespace NaijaEvent.Service
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

   //     services.AddMediatR(typeof(GetNEventDetailsHandler).GetTypeInfo().Assembly);

        services.AddAutoMapper(typeof(NEventProfile).Assembly);
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<CreateEventCommandValidator>());
        services.AddDbContext<NaijaEventContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("NaijaEventDatabase")));

services.AddMediatR(typeof(CreateEventCommand).Assembly);
```

Most helpful comment

Please replace Mediator with IMediator

    private readonly IMediator _mediator;
    public EventController(IMediator mediator)
    {
        _mediator = mediator;
    }

All 3 comments

I'm not seeing anything wrong - do you have a link to a more complete example?

Hello Jimmy

see link to the repository

https://github.com/etechnologiesng/NaijaEventAPICleanArchitecture

Please help

Please replace Mediator with IMediator

    private readonly IMediator _mediator;
    public EventController(IMediator mediator)
    {
        _mediator = mediator;
    }
Was this page helpful?
0 / 5 - 0 ratings