首页 > 解决方案 > MediatR 未在 api 控制器中解析

问题描述

我已经添加MediatROnApplicationStartedglobal.asax

但这并不能解决我的控制器。

它返回一个错误:

{
  "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)"
  }
}

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();
    }
}

控制器:

public class SearchResultsApiController : UmbracoApiController
{
    private readonly IMediator _mediator;

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

我正在使用 .NET 4.7.2(如果重要的话,还有 Umbraco 7.15.3)。

标签: asp.netdependency-injectionglobal-asaxasp.net-apicontrollermediatr

解决方案


问题是我在两个单独的项目中有 api 控制器,所以解析器可能找不到合适的。

如果一个项目中的 api 控制器正常工作,则第二个项目中的控制器无法显示上述错误。

我已将所有 api 控制器合并到一个项目中,现在一切正常。


推荐阅读