微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MediatR无法在api控制器中解析

如何解决MediatR无法在api控制器中解析

我在MediatR的{​​{1}}中添加OnApplicationStarted

但是它不能解决我的控制器。

它返回错误

global.asax

global.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,Type controllerType)"
  }
}

WebApiConfig

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;

控制器:

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

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

解决方法

问题是我在两个不同的项目中都有api控制器,所以解析器可能找不到正确的项目。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。