最新版本的 Ninject.Web.Mvc 使用瞬态作用域来注册控制器RegisterAllControllersIn
:
public void RegisterAllControllersIn(Assembly assembly,
Func<Type, string> namingConvention)
{
foreach (Type type in assembly.GetExportedTypes().Where(IsController))
_kernel.Bind<IController>()
.To(type)
.InTransientScope()
.Named(namingConvention(type));
}
我也去NinjectControllerFactory
上课了。它的 CreateController 函数非常基本。它在内核上为控制器执行 TryGet 并返回它返回的内容——如果找不到控制器,它会委托给基类:
public override IController CreateController(RequestContext requestContext,
string controllerName)
{
var controller = Kernel.TryGet<IController>(controllerName.ToLowerInvariant());
if (controller == null)
return base.CreateController(requestContext, controllerName);
var standardController = controller as Controller;
if (standardController != null)
standardController.ActionInvoker = new NinjectActionInvoker(Kernel);
return controller;
}
因此,基于绑定设置和工厂,它似乎没有在 Singleton 范围内创建对象。您可以做的一件事是在创建内核后编写一些调试代码并自己检查绑定以确认范围是什么。我做了一个小实验,并将代码添加到我的 HttpApplication 类中,如下所示。完全披露,这是使用 ASP.Net MVC 1.0,所以你的里程可能会有所不同。如果有机会,我会获取最新的 MVC 2 预览版并尝试相同的实验。
protected void DumpBindings() {
var bindings = Kernel.GetBindings(typeof(IController));
var dummyRequest = new RequestContext(
new HttpContextWrapper(HttpContext.Current),
new RouteData());
foreach (var binding in bindings) {
var scope = "Custom";
if (binding.ScopeCallback == StandardScopeCallbacks.Request)
scope = "Request";
else if (binding.ScopeCallback == StandardScopeCallbacks.Singleton)
scope = "Singleton";
else if (binding.ScopeCallback == StandardScopeCallbacks.Thread)
scope = "Thread";
else if (binding.ScopeCallback == StandardScopeCallbacks.Transient)
scope = "Transient";
HttpContext.Current.Trace.Write(
string.Format(
"Controller: {0} Named: {1} Scope: {2}",
binding.Service.Name,
binding.Metadata.Name,
scope));
var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
var controller1 = controllerFactory.CreateController(
dummyRequest, binding.Metadata.Name);
var controller2 = controllerFactory.CreateController(
dummyRequest, binding.Metadata.Name);
HttpContext.Current.Trace.Write(
string.Format(
"{0} controller1 == {0} controller2 ? {1}",
binding.Metadata.Name,
object.Equals(controller1, controller2)));
}
}
我RegisterAllControllersIn
在OnApplicationStarted
. 它在跟踪输出中创建了以下消息:
控制器:IController 名称:home
范围:Transient home controller1
== home controller2 ? 假控制器:IController 名称:帐户
范围:瞬态帐户控制器
1 == 帐户控制器 2?错误的
因此,所有这些都是确认正在使用瞬态范围,并且控制器工厂在请求时返回同一控制器的不同实例。所以,我唯一能想到的是:
- 也许您没有使用最新版本的 Ninject 2 和 Ninject.Web.Mvc
- 问题出在 MVC 级别——即它重用了工厂创建的控制器