问题
There are a half dozen identical questions, I know. I've followed the advice of their answers, and I'm still stuck.
I've used NuGet to install
Ninject.Web.MVC4
and added my bindings toNinjectWebCommon.RegisterServices
.I've manually added the following to system.web/httpModules
<httpModules> <add name="OnePerRequestModule" type="Ninject.OnePerRequestModule"/> </httpModules>
- I've wrapped my bindings in a Ninject
GlobalKernelRegistrationModule<OnePerRequestHttpModule>
My DataContext:
public class DataContext : DbContext, IDataContext
{
private Guid guid;
public DataContext()
{
guid = Guid.NewGuid();
Trace.TraceInformation("DataContext Constructing " + guid.ToString());
}
protected override void Dispose(bool disposing)
{
Trace.TraceInformation("DataContext Disposing " + guid.ToString());
base.Dispose(disposing);
}
}
My binding:
Bind<IDataContext>().To<DataContext>().InRequestScope();
And my output, after clicking a few links on my site:
DataContext Constructing aa349c66-1a64-4037-a7cb-8b51974b86fe
DataContext Disposing aa349c66-1a64-4037-a7cb-8b51974b86fe
DataContext Constructing b37c4a4b-6f6a-4d43-94c6-ae701af91e43
DataContext Constructing ef388648-2b9e-40dd-8542-48c008df61d4
DataContext Constructing 18e245fd-e09c-49da-b901-4f59d770d130
DataContext Constructing 50d86e33-5e17-4a6c-81df-f6a0d49591ab
DataContext Constructing 87ad5075-ad0d-4d30-a200-b211764a25ef
Any suggestions as to where I should go from here?
来源:https://stackoverflow.com/questions/25923285/ninject-web-mvc4-disposes-my-dbcontext-exactly-once