gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:47:24 24 4
gpt4 key购买 nike

尝试通过 Steve Sanderson 的 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-mvc - SportStore : WebUI. WindsorControllerFactory.GetControllerInstance(System.Type:找不到合适的方法来覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2038406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com