【问题标题】:Castle.Windsor - Dependency Injection of the parameter constructor on calling parameterless constructorCastle.Windsor - 参数构造函数在调用无参数构造函数时的依赖注入
【发布时间】:2013-12-13 09:28:09
【问题描述】:

我们正在使用 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);
.
.
.
}
}

【问题讨论】:

  • 为什么不从 Windsor 容器初始化控制器呢?然后将为您解决依赖关系。
  • @stuartd,我是在 Global.asax 中完成的,请检查我更新的问题,但它仍然返回 null。

标签: dependency-injection castle-windsor


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多