The most recent version of Ninject.Web.Mvc is using a transient scope to register the controllers in 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));
}
I looked into the the NinjectControllerFactory
class as well. Its CreateController function is pretty basic. It does a TryGet on the kernel for the controller and returns what it gets back -- if it can t find the controller, it delegates to the base class:
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;
}
So, based on the binding setup and based on the factory, it would seem it s not creating objects in Singleton scope. One thing you could do is write a little debug code after you create your kernel and check the bindings yourself to confirm what the scope is. I did a little experiment and added the code to my HttpApplication class show below. Full disclosure, this is using ASP.Net MVC 1.0, so your mileage may vary. If I have the opportunity, I will get the latest MVC 2 preview and try the same experiment.
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)));
}
}
I called this right after the call to RegisterAllControllersIn
in the OnApplicationStarted
. It created the following messages in the trace output:
Controller: IController Named: home
Scope: Transient home controller1
== home controller2 ? False Controller: IController Named: account
Scope: Transient account controller1
== account controller2 ? False
So, all this does is confirm that transient scope is being used and that the controller factory is returning a different instance of the same controller when requested. So, the only thing I can think of is that:
- Perhaps you are not using the latest builds of Ninject 2 and Ninject.Web.Mvc
- The issue is at the MVC level -- i.e. it s reusing the controller created by the factory