Castle.Windsor - 参数构造函数的依赖注入调用无参数构造函数

时间:2013-12-13 09:28:09

标签: dependency-injection castle-windsor

我们正在使用Castle.Windsor进行asp.net mvc(C#)应用程序的依赖注入。

如何在调用无参数构造函数时解析参数构造函数中声明的依赖项?

以下是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));
        }

从Global.asax,我们调用ErrorController,它调用无参数构造函数,但它无法解析在ErrorController中使用的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);
        }

下面是控制器和服务注册的代码以及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);
.
.
.
}
}

1 个答案:

答案 0 :(得分:1)

您可以让Castle为您解析控制器:

switch (httpException.GetHttpCode())
{
      case 404:
      errorController = container.Resolve<ErrorController>();
      routeData.Values.Add("controller", "Error");
      …