asp.net-mvc – SportStore:WebUI.WindsorControllerFactory.GetControllerInstance(System.Type:找不到合适的方法来覆盖

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – SportStore:WebUI.WindsorControllerFactory.GetControllerInstance(System.Type:找不到合适的方法来覆盖前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
试图通过Steve Sandersons的MVC书 – 但是在创建WindsorControllerFactory时遇到了困难.看起来该方法已从MVC1更改为MVC2.这是我在尝试编译项目时遇到的错误

‘WebUI.WindsorControllerFactory.GetControllerInstance(System.Type:找不到合适的方法来覆盖’.任何帮助都会受到赞赏 – 我无法超越这个!

这是代码 – 从书中转录:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using System.Reflection;
using Castle.Core;
using Castle.MicroKernel;
namespace WebUI
{
    public class WindsorControllerFactory : DefaultControllerFactory
    {
        WindsorContainer container;
        // The constructor:  
        // 1. Sets up a new IoC container  
        // 2. Registers all components specified in web.config  
        // 3. Registers all controller types as components  
        public WindsorControllerFactory()
        {
            // Instantiate a container,taking configuration from web.config  
            container = new WindsorContainer(
                new XmlInterpreter(new ConfigResource("castle"))
                );
            // Also register all the controller types as transient  
            var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                  where typeof(IController).IsAssignableFrom(t)
                                  select t;
            foreach (Type t in controllerTypes)
                container.AddComponentWithLifestyle(t.FullName,t,LifestyleType.Transient);
        }
        // Constructs the controller instance needed to service each request  
        protected override IController GetControllerInstance(Type controllerType)
        {
            return (IController)container.Resolve(controllerType);
        }
    }
}


问候,
马丁

解决方法

由于有关竞争条件的不幸错误,GetControllerInstance已从ASP.NET MVC 1.0更改为ASP.NET MVC 2.

ASP.NET MVC 1.0中的签名是:

protected virtual IController GetControllerInstance(
    Type controllerType);

在ASP.NET MVC 2中它是:

protected virtual IController GetControllerInstance(
    RequestContext requestContext,Type controllerType)

对于这种特殊情况,您似乎只需要将方法的签名更改为:

protected override IController GetControllerInstance(
        RequestContext requestContext,Type controllerType) 
    { 
        return (IController)container.Resolve(controllerType); 
    }

潜在的竞争条件是RequestContext实例可以由多个同时请求共享,这将是一个主要的禁忌.幸运的是,似乎没有任何用户遇到过这个问题,但无论如何它在ASP.NET MVC 2中得到了修复.

猜你在找的asp.Net相关文章