0

I am trying to implement mediatr pattern in ASP.Net Web Api.

Getting the following error:

Handler was not found for request of type MediatR.IRequestHandler`2[Orion_API.Campaigns.GetCampaigns.GetCampaignsRequest,System.Collections.Generic.List`1[Orion_API.Campaigns.GetCampaigns.GetCampaignsResponse]]. Register your handlers with the container.

My bootstrapper class is as follows:

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

builder.RegisterGeneric(typeof(GenericRepository<>))
       .As(typeof(IGenericRepository<>))
       .InstancePerRequest();

builder.RegisterType<Mediator>().As<IMediator>().InstancePerLifetimeScope();

builder.Register<ServiceFactory>(context =>
{
    var componentContext = context.Resolve<IComponentContext>();
    return t => {
        object o;

        return componentContext.TryResolve(t, out o) ? o : null;
    };
});


//Set the dependency resolver to be Autofac.  
Container = builder.Build();
Peter Hull
  • 6,683
  • 4
  • 39
  • 48
Atish Jadhav
  • 61
  • 1
  • 2
  • 4
  • You can have a look at https://github.com/jasontaylordev/NorthwindTraders/blob/master/Src/Application/DependencyInjection.cs and of course the whole project – Sir Rufo Mar 20 '20 at 08:05
  • From where does the application calls "public static IServiceCollection AddApplication(this IServiceCollection services)" method. – Atish Jadhav Mar 20 '20 at 10:03
  • From https://github.com/jasontaylordev/NorthwindTraders/blob/master/Src/WebUI/Startup.cs#L37 – Sir Rufo Mar 20 '20 at 10:56
  • BTW you can find that by searching for AddApplication in the github search box (top left) – Sir Rufo Mar 20 '20 at 10:58

1 Answers1

0

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

    builder.RegisterGeneric(typeof(GenericRepository<>))
           .As(typeof(IGenericRepository<>))
           .InstancePerRequest();

    builder.RegisterType<Mediator>().As<IMediator>().InstancePerLifetimeScope();

    builder.Register<ServiceFactory>(context =>
    {
        var componentContext = context.Resolve<IComponentContext>();
        return t => {
            object o;

            return componentContext.TryResolve(t, out o) ? o : null;
        };
    });
  • can you explain why the code you have posted solved the problem and what was the problem with the original code? – yonBav Mar 20 '20 at 09:36