尝试通过史蒂夫·桑德森的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);
}
}
}
++++ 问候, 马丁
答案 0 :(得分:14)
由于关于竞争条件的不幸错误,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中得到修复。
答案 1 :(得分:1)
在MVC2中,此方法的签名如下:
protected internal virtual IController GetControllerInstance(
RequestContext requestContext,
Type controllerType
)
(取自MSDN。)