2

我创建了自己的项目,但我无法使用城堡启动我的接口的实现!我添加了我的ControllersInstaller,WindsorControllerFactory类,如下所示。我的控制器是下面的最后一段代码。请帮忙!谢谢,詹姆斯

public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public override void ReleaseController(IController controller)
    {
        kernel.ReleaseComponent(controller);
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
        }
        return (IController)kernel.Resolve(controllerType);
    }
}



public class ControllersInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromThisAssembly()
                        .BasedOn<IController>()
                        .LifestyleTransient());
    }
}






 public class HomeController : Controller
{
    public IMemberMapper _memberMapper;

    public HomeController()
    {
    }

    public HomeController(IMemberMapper memberMapper)
    {
        _memberMapper = memberMapper;
    }

    public ActionResult Index()
    {
        var model = _memberMapper.Map();
        return View(model);
    }
}

// 全局文件

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }

    private static IWindsorContainer container;

    private static void BootstrapContainer()
    {
        container = new WindsorContainer()
            .Install(FromAssembly.This());
        var controllerFactory = new WindsorControllerFactory(container.Kernel);
        ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    }

    protected void Application_End()
    {
        container.Dispose();
    }
}
4

2 回答 2

0

你注册WindsorControllerFactory为你的现任了ControllerFactory吗?

这通常在 Globals.asax.cs 中完成。如果你没有这样做,看看这个例子:https ://github.com/kkozmic/ToBeSeen/blob/master/src/ToBeSeen/Global.asax.cs

更具体地说,查看BootstrapContainer()方法及其调用位置。这指示 ASP.NET MVC 尝试使用您新定义的工厂创建它可能需要的任何控制器。如果没有这一步,你只是在使用内置的控制器工厂。

于 2012-12-26T18:36:42.563 回答
0

您必须在IMemberMapper某处注册您的服务。最好的地方是为此目的的另一个安装程序类。

于 2012-12-27T11:07:05.920 回答