eddy eddy - 1 month ago 12
C# Question

How to configure Ninject so that it creates a single instance per Controller

I'm trying to use Ninject for an MVC5 application and I want to know if there's any way I can configure it so that instantiate a single object per

Controller
(I'm not sure if it the same as per request), use that instance in any
Action
of the controller and once the
Action
has finished release all the resources that the object utilized.

The class that I want Ninject to inject in my controllers implements the IDisposable interface.

So far I have tried this:

private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUnitOfWork>().To<SqlUnitOfWork>();
}


But the
Dispose
method never gets called. I also tried
InRequestScope
, but it didn't work either.

So far the only thing that has actually worked is wrapping the instantiation of the object that I need in a
using
statement in EVERY Controller's
Action
.

Something like this:

using (var uow = UnitOfWorkFactory.Create())
{
var repos = new UserRepository(uow);

uow.SaveChanges();
}


EDIT:



The NinjectWebCommon class added by the Ninject package

public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();

/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}

/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}

/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUnitOfWork>().To<SqlUnitOfWork>().InRequestScope();
}
}


As you can see from the code above, now I'm using
kernel.Bind<IUnitOfWork>().To<SqlUnitOfWork>().InRequestScope();
, but the
Dispose
method of my class still never gets called.

Is there really any way to achieve with Ninject the same behaviour we get with a
using
statement, that is, when the object created is done being used (the last line of code in the
Controller
's
Action
is processed) the
Dispose
method of that object is called?

Answer

There's a Module within Ninject that needs to be installed, configured (and up to date)[https://github.com/ninject/ninject/issues/132#issuecomment-42459686] for this to be done deterministically at the end of each request (vs driven by GC).