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中得到了修复.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....