1

I have created a site with MVC5/ EF6 and am using Ninject as the DI container to bind it together.

All is working well except on one page, where i am making 2 ajax calls at the same time.

The controller/repository spits out various errors such as:

"A first chance exception of type 'System.Data.Entity.Core.EntityException' occurred in mscorlib.dll. Additional information: The underlying provider failed on Open."

One call to the api will always work, the second works intermittently. If i create the datacontext directly in the controller, it all works fine, so the error must relate to Ninject, but i can't work out what i need to do to sort it. Varying the scope to Transient/Thread just changes the error.

Any help much appreciated.

Here is my (edited) code:

Called from Global.asax Application_Start()

var context = new myDataContext();

// Lowest down wins
IKernel kernel = new StandardKernel();

DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

GlobalConfiguration.Configuration.DependencyResolver
    = new App_Start.NinjectResolver(kernel);

kernel.Bind<IRepository<Models.MyClass>>()
    .To<MyRepository>()
    //.InTransientScope()
    //.InThreadScope()
    .InRequestScope()      
    .WithConstructorArgument("context", context);

Api Controller

  public class MyApiController : ApiController
  {
    IRepository<MyRepository> _repository;

    public EMAAFController(IRepository<MyRepository> repository)
    {
     _repository = repository;
    }



    public IEnumerable<Models.MyClass> Get(int id)
    {
      var itemToReturn = _repository.GetAll().Where(i => i.id == id).ToList();

      return itemToReturn;
    }
  }
JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123

1 Answers1

2

You are doing it wrong. Binding is done on application start and in your case you create instance of context and path it to binding

change your binding to

kernel.Bind<myDataContext>().ToSelf().InRequestScope();

kernel.Bind<IRepository<Models.MyClass>>().To<MyRepository>().InRequestScope()   

so when ninject will resolve your repository it will see constructor with input parameter myDataContext and will understand how to get context

Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • Thank you for the answer. It seems like it is going to be right, but i am still confused as to where the instance of the datacontext will be created. Please could you spell it out for me. I have tried about 20 mins googling and can't work out the magic. – JsAndDotNet Jan 29 '14 at 21:19
  • Ignore me. I have worked it out. In addition to your point, my constructor was tring to take in a DbContext, rather than an interface or specific context. This stopped Ninject from resolving the binding. Having a constructor in the repository that took in MyDataContext fixed it. Thanks for the help – JsAndDotNet Jan 29 '14 at 21:35
  • If any searchers find this post and have read my previous comment, I also found this helped - http://stackoverflow.com/questions/14840515/no-matching-bindings-are-available-and-the-type-is-not-self-bindable-in-ninject – JsAndDotNet Jan 29 '14 at 21:44