繁体   English   中英

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

[英]SportStore: WebUI.WindsorControllerFactory.GetControllerInstance(System.Type: no suitable method found to override

试图通过史蒂夫·桑德森的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中得到了修复。

在MVC2中,此方法的签名如下:

protected internal virtual IController GetControllerInstance(
    RequestContext requestContext,
    Type controllerType
)

(摘自MSDN 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM