2

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?

epalm
  • 4,283
  • 4
  • 43
  • 65
  • maybe this is related: https://github.com/ninject/ninject/issues/147 have you made sure you're using the latest ninject.web.common? (v3.2.2, not 3.2.0!) – BatteryBackupUnit Sep 19 '14 at 05:24
  • Why are you adding http modules? You shouldn't be... There is no configuration required, other than your bindings when using Ninject.MVC4 – Erik Funkenbusch Sep 19 '14 at 16:02
  • @BatteryBackupUnit I reinstalled Ninject.MVC4 and all its dependencies, and while just adding my bindings to `NinjectWebCommon.RegisterServices` didn't work, wrapping them in a `GlobalKernelRegistrationModule` worked! – epalm Sep 19 '14 at 16:16
  • @ErikFunkenbusch If just the binding worked as advertised, I wouldn't be asking here. Adding http modules was a suggestion from a similar Q/A that I followed. – epalm Sep 19 '14 at 16:17
  • @epalm - the http module should not be used when using Ninject.Web.Common as they do the same thing. You end up registering twice! The HttpModule is the "old" way to do it. Ninject.Web.Common is the new "configurationless" way. See https://github.com/ninject/Ninject.Web.Common/wiki/InRequestScope – Erik Funkenbusch Sep 19 '14 at 16:18
  • @ErikFunkenbusch I should have mentioned that attempt #2 was performed separately from attempt #1. Sorry for the confusion. – epalm Sep 19 '14 at 16:26
  • @epalm - something is apparently holding onto a reference to your dc. I would make sure you don't have any static variables, cache, or other items which might contain references. – Erik Funkenbusch Sep 19 '14 at 16:29

0 Answers0