1
我們使用Castle.Windsor進行我們的asp.net mvc(C#)應用程序的依賴注入。Castle.Windsor - 依賴注入調用無參數構造函數的參數構造函數
如何解決在調用無參數構造函數時在參數構造函數中聲明的依賴?
以下是ErrorController代碼:
public class ErrorController : BaseController
{
#region Declaration
private readonly ICommonService _commonService;
#endregion
#region Constructor
public ErrorController()
{
}
public ErrorController(ICommonService commonService)
: base(commonService)
{
this._commonService = commonService;
}
#endregion
.
.
.
}
以下是在Global.asax的代碼,用於如果出現任何錯誤顯示自定義錯誤頁:
protected void Application_Error()
{
Exception lastException = Server.GetLastError();
HttpException httpException = lastException as HttpException;
RouteData routeData = new RouteData();
//Possible reason for null is the the server error may not be a proper http exception
if (httpException == null)
{
errorController = new ErrorController();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
Response.Clear();
}
else
//It's an Http Exception, Let's handle it.
{
switch (httpException.GetHttpCode())
{
case 404:
//Page not found.
//Call target Controller
errorController = new ErrorController();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "PageNotFound");
break;
case 302:
//Page Temporarily Moved
errorController = new ErrorController();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "PageNotFound");
break;
case 500:
//Server error.
errorController = new ErrorController();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
Response.Clear();
break;
default:
errorController = new ErrorController();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
Response.Clear();
break;
}
}
//Pass exception details to the target error View.
routeData.Values.Add("error", lastException);
//Clear the error on server.
Server.ClearError();
//Call target Controller and pass the routeData.
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
從全局。 asax,我們調用ErrorController調用無參數構造函數,並且它無法解決ErrorService中用於從數據庫收集一些基本數據的CommonService的依賴性。但是_commonService沒有被初始化,並且它的拋出null。請建議如何處理這種情況。
更新
下面就是我所說的依賴注入在Global.asax
protected void Application_Start()
{
.
.
BootstrapContainer();
}
private static void BootstrapContainer()
{
container = new WindsorContainer().Install(
new ServiceInstaller(),
FromAssembly.This()
);
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
下面的代碼是在控制器和服務是regsitered和BootstrapContainer代碼:
public class ControllersInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestyleTransient());
}
}
public class ServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<ICommonService>().ImplementedBy<CommonService>().LifeStyle.PerWebRequest);
.
.
.
}
}
爲什麼不從Windsor容器初始化控制器?然後,依賴關係將爲您解決。 – stuartd
@stuartd,我在Global.asax中做過,請檢查我更新的問題,但仍然返回null。 – Prasad