2
我正在研究这个(http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Five-Adding-logging-support.ashx)Castle Windsor的IoC教程,我已经通过了上述所有步骤,并且如上面的教程中所述,我试图让log4net记录仪通过属性注入到我的控制器,如下列:Castle Windsor IoC不会将log4net注入到我的控制器中
public class HomeController : Controller
{
// this is Castle.Core.Logging.ILogger, not log4net.Core.ILogger
public ILogger Logger { get; set; }
public ActionResult Index()
{
Logger.Debug("Hello world");
ViewBag.Message = "Hello world";
return View();
}
}
但不幸的是,在Logger.Debug
记录仪的执行点为空,使之与空引用异常的结果。虽然当我试图呼叫
var logger = container.Resolve<ILogger>();
logger.Debug("Container bootstrapped");
内部Global.asax记录器是完全解决。
为什么温莎不想解决控制器内部的依赖关系?
EDIT
控制器经由温莎
Global.asax中创建
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Castle.Core.Logging;
using Castle.Windsor;
using Castle.Windsor.Installer;
using FlightManagementSystem.WebPlatform.Configuration.IoC.Windsor.Factories;
namespace FlightManagementSystem.WebPlatform
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
private static IWindsorContainer container;
protected void Application_Start()
{
Debugger.Break();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
container = new WindsorContainer();
container.Install(FromAssembly.This());
var controllerFactory = new ControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
protected void Application_End()
{
container.Dispose();
}
}
}
ControllerFactory.cs
个using System;
using System.Web;
using System.Web.Mvc;
using Castle.MicroKernel;
namespace FlightManagementSystem.WebPlatform.Configuration.IoC.Windsor.Factories
{
public class ControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
public ControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
public override void ReleaseController(IController controller)
{
kernel.ReleaseComponent(controller);
}
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, String.Format(Resources.THE_CONTROLLER_FOR_PATH_0_COULD_NOT_BE_FOUND, requestContext.HttpContext.Request.Path));
}
return (IController) kernel.Resolve(controllerType);
}
}
}
ControllerInstaller.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
namespace FlightManagementSystem.WebPlatform.Configuration.IoC.Windsor.Installers
{
public class ControllerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register
(
Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestyleTransient()
);
}
}
}
LoggerInstaller.cs
using Castle.Facilities.Logging;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
namespace FlightManagementSystem.WebPlatform.Configuration.Logger.log4net
{
public class LoggerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<LoggingFacility>(f => f.UseLog4Net());
}
}
}
你如何创建控制器? – 2013-05-12 21:24:09
控制器通过Windsor创建,我编辑了主要问题,插入了控制器创建逻辑 – Lu4 2013-05-12 21:29:31
您是否添加并配置了“LoggingFacility”? – 2013-05-12 21:34:59