加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – SportStore:WebUI.WindsorControllerFactory.Ge

发布时间:2020-12-16 04:10:44 所属栏目:asp.Net 来源:网络整理
导读:试图通过Steve Sandersons的MVC书 – 但是在创建WindsorControllerFactory时遇到了困难.看起来该方法已从MVC1更改为MVC2.这是我在尝试编译项目时遇到的错误: ‘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中得到了修复.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读