2017-09-06 66 views
0

我們有一個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。

回答

1

你的一些構造函數做得比它應該做得更多,並且當它的父類型被解析時導致錯誤。

您需要檢查可疑控制器CashAccountFlowController的依賴關係,並驗證其對象圖是否完全沒有錯誤地被解析。

當框架無法通過具有無法安全提供的依賴關係的構造函數正確初始化控制器時,將返回標準錯誤消息。

SO檢查以確保圖中對象的構造函數沒有執行可能導致發生錯誤的邏輯。

+0

Hi NKosi, 1.這是任何應用程序的典型場景,Controller => Service Layer => Repository => EntityFramework DB Context。所以,當控制器初始化時,所有的依賴關係都會被加載。你能幫我,我該怎麼做才能避免這個問題。 2.您能否請建議一些工具來檢查和驗證對象圖完全解決沒有錯誤? – Pandiarajan

相關問題