Ninject.Web.MVC4 disposes my DbContext exactly once

心已入冬 提交于 2020-01-04 14:12:31

问题


There are a half dozen identical questions, I know. I've followed the advice of their answers, and I'm still stuck.

  1. I've used NuGet to install Ninject.Web.MVC4 and added my bindings to NinjectWebCommon.RegisterServices.

  2. I've manually added the following to system.web/httpModules

<httpModules>
  <add name="OnePerRequestModule" type="Ninject.OnePerRequestModule"/>
</httpModules>
  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!