title: What is the scope of my Ninject injected ObjectContext in my custom MembershipProvider (using Request scope)?
body: <p>I use Entity Framework 4 and ASP.NET MVC 3. I made a custom membership provider and use Ninject to inject an EFAccountRepository into it (Bound IAccountRepository to EFAccountRepository). </p>
  
  <p>This account repository has an ObjectContext injected into it. I also use this repository (and others) in my controllers. For this reason when I bound IContext to my ObjectContext, I set the scope to "per request" so the ObjectContext only lives in one request and is shared between the repositories.</p>
  
  <p>I am sometimes get the following error when trying to log in:"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."</p>
  
  <p>I wonder how often the membership provider gets instantiated. I injected the repository into the membership provider by marking the repository property with <code>[Inject]</code> and calling <code>Kernel.Inject</code> in the <code>Application_Start</code> function in the global.asax file.</p>
  
  <p>If the provider gets instantiated more than once I would have to inject again I suppose. However, I don't get a null pointer exception, so I don't think that's it. </p>
  
  <h2>Update 1</h2>
  
  <p>Here's some code:</p>
  
  <p><strong>MyNinjectModule.cs</strong></p>
  
  <pre><code>    public override void Load()
      {
          Bind&lt;IMyContext&gt;().To&lt;MyObjectContext&gt;().InRequestScope();
          // put bindings here
          Bind&lt;IAccountRepository&gt;().To&lt;EFAccountRepository&gt;
      }
  </code></pre>
  
  <p><strong>Global.asax</strong></p>
  
  <pre><code>    protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
  
          RegisterGlobalFilters(GlobalFilters.Filters);
          RegisterRoutes(RouteTable.Routes);
  
          var kernel = new StandardKernel(new MyNinjectModule());
          ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(kernel));
          kernel.Inject(Membership.Provider);
      }
  </code></pre>
  
  <p><strong>MyMembershipProvider.cs</strong></p>
  
  <pre><code>    [Inject]
      public IAccountRepository accountRepository { get; set; }
  
      public override bool ValidateUser(string username, string password)
      {
          // I get the exception here.
          return (from a in accountRepository.Accounts
                  where a.UserName == username 
                   &amp;&amp; a.Password == password
                  select true).SingleOrDefault();
      }
  </code></pre>
  
  <p><strong>EFAccountRepository.cs</strong></p>
  
  <pre><code>    private readonly IMyContext context;
  
      public EFAccountRepository(IMyContext context)
      {
          this.context = context;
      }
  
      public IQueryable&lt;Account&gt; Accounts
      {
          get { return context.Accounts; }
      }
  </code></pre>
  
  <p><strong>MyObjectContext.cs</strong></p>
  
  <pre><code>public class MyObjectContext : ObjectContext, IMyContext
  {
      public IObjectSet&lt;Account&gt; Accounts { get; private set; }
  
      public FlorenceObjectContext()
          : this("name=DomainModelContainer")
      {
      }
  
      public FlorenceObjectContext(string connectionString)
          : base(connectionString, "DomainModelContainer")
      {
          Accounts = CreateObjectSet&lt;Account&gt;();
      }
  }
  </code></pre>
  
  <p>PS: I'm always open to comments on my code in general ;).</p>