我们有一个web api 2服务。我正在使用Unity进行依赖注入,并使用Entityframework进行ORM。我在Unity中分别注册类,如下面的代码部分所述。每周,我收到此错误WebApi 2控制器创建仅在生产中一周失败
尝试创建类型为 'CashAccountFlowController'的控制器时发生错误。确保控制器具有 无参数公共构造函数。在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,类型 controllerType)在 System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage 请求)在 System.Web.Http .Dispatcher.HttpControllerDispatcher。 <SendAsync> d__1.MoveNext() 类型 'Analytical.Controller.CashAccountFlowController' 根本不System.Linq.Expressions.Expression.New(类型 型)在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator有 默认构造函数.GetInstanceOrActivator(HttpRequestMessage 请求,类型controllerType,Func`1 &活化剂)在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,类型 controllerType)
直到上周,我每个星期一都会收到这个错误。每个周日晚上我们都会做iisreset。但是这个星期我在星期二得到了这个错误。所有其余的日子,它工作正常。此服务在周末不使用。
我的控制器:
public IHttpActionResult Post([FromBody] CashAccountContract cashAccountContract)
public CashAccountFlowController(IAnalyticalInformationService analyticalInformationService,
IDtoDomainMapper dtoDomainMapper) : base(dtoDomainMapper)
{
_analyticalInformationService = analyticalInformationService;
}
在回转流服务指的UnitOfWork,然后的UnitOfWork指的存储库。
统一依赖注入注册。
private void Register()
{
//Register Repositories
_unityContainer.RegisterType<IEntityDomainMapper, EntityDomainMapper>();
_unityContainer.RegisterType<AnalyticalServiceEntities, AnalyticalServiceEntities>();
_unityContainer.RegisterType<DbContext, AnalyticalServiceEntities>();
_unityContainer.RegisterType<IConversationRepository, ConversationRepository>(
new HierarchicalLifetimeManager());
_unityContainer.RegisterType<IAnalyticalRepository, AnalyticalRepository>(new HierarchicalLifetimeManager());
_unityContainer.RegisterType<ICustomerRepository, CustomerRepository>(new HierarchicalLifetimeManager());
_unityContainer.RegisterType<IBoxRepository, BoxRepository>(new HierarchicalLifetimeManager());
_unityContainer.RegisterType<ICashAccountSeriLogRepository, CashAccountSeriLogRepository>(new HierarchicalLifetimeManager());
_unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
//Register Services
_unityContainer.RegisterType<IAnalyticalInformationService, AnalyticalInformationService>(
new HierarchicalLifetimeManager());
_unityContainer.RegisterType<IAnalyticalDataOutstandingService, AnalyticalDataOutstandingService>(
new HierarchicalLifetimeManager());
_unityContainer.RegisterType<IAnalyticalIntradayService, AnalyticalIntradayService>(
new HierarchicalLifetimeManager());
_unityContainer.RegisterType<IUserService, UserService>(
new HierarchicalLifetimeManager());
//Register Controllers
_unityContainer.RegisterType<IDtoDomainMapper, DtoDomainMapper>();
_unityContainer.RegisterType<ICashAccountFlowController, CashAccountFlowController>();
}
此前,我尝试HierarchicalLifeTimeManager以及瞬态(如上),但没有人没有帮助我。
我无法在本地重现它。 请帮助我为什么它在生产中每周失败ONLY ONCE
。
在此先感谢,
Pandiarajan。
Hi NKosi, 1.这是任何应用程序的典型场景,Controller => Service Layer => Repository => EntityFramework DB Context。所以,当控制器初始化时,所有的依赖关系都会被加载。你能帮我,我该怎么做才能避免这个问题。 2.您能否请建议一些工具来检查和验证对象图完全解决没有错误? – Pandiarajan