MediatR not resolving in an api controller

450 views Asked by At

I have added MediatR to OnApplicationStarted in global.asax

But it's not resolving for my controller.

It returns an error:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "An error occurred when trying to create a controller of type 'NotificationApiController'. Make sure that the controller has a parameterless public constructor.",
  "ExceptionType": "System.InvalidOperationException",
  "StackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
  "InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "Type 'MyDomain.MyProject.Controllers.NotificationApiController' does not have a default constructor",
    "ExceptionType": "System.ArgumentException",
    "StackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
  }
}

The global.asax:

var builder = new ContainerBuilder();

/* MVC Controllers */
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();

/* WebApi Controllers */
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

/* Umbraco Controllers */
builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);

/* Custom Api Controllers */
builder.RegisterApiControllers(typeof(Controllers.SearchResultsApiController).Assembly);

builder.RegisterModule<WebApiConfig>();

var container = builder.Build();

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver =
    new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy =
    IncludeErrorDetailPolicy.Always;

WebApiConfig:

public class WebApiConfig : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        // Register custom types with Autofac

        /* Third-party types */
        // This didn't work so I added the below line with the explicit handler
        builder.AddMediatR(this.GetType().Assembly);
        
        // But it didn't make any difference
        builder.AddMediatR(typeof(Index).Assembly);

        /* Umbraco context types */
        ApplicationContext applicationContext = ApplicationContext.Current;
        builder.RegisterInstance(applicationContext.Services.ContentService)
            .As<IContentService>();
        builder.RegisterInstance(applicationContext.Services.MemberService)
            .As<IMemberService>();

        //builder.Register(c => UmbracoContext.Current).AsSelf();
        builder.Register(c => UmbracoContext.Current).InstancePerRequest();
        builder.Register(x => new UmbracoHelper(UmbracoContext.Current))
            .InstancePerRequest();
    }
}

The controller:

public class SearchResultsApiController : UmbracoApiController
{
    private readonly IMediator _mediator;

    public SearchResultsApiController(IMediator mediator)
    {
        _mediator = mediator;
    }
}

I'm using .NET 4.7.2 (and Umbraco 7.15.3 if that matters).

1

There are 1 answers

0
nickornotto On

The issue was that I had api controllers in two separate projects so probably the resolver couldn't find the right one.

If api controllers in one project were working, the controllers in the second project were failing showing the above error.

I have consolidated all api controllers in one project and now everything works fine.