c# - How can I configure Ninject to inject my DbContext for use with ASP.NET Identity Framework? -


i'd use ninject inject dbcontext asp.net identity framework.

at moment have following line in web.config file:

<appsettings>     <add key="owin:appstartup" value="owintest.identity.owinstart, owintest.identity" /> </appsettings> 

this causes configuration() method on class owinstart in owintest.identity project called.

here's owinstart class:

public class owinstart     {         public void configuration(iappbuilder app)         {             app.createperowincontext<imycontext>(this.getdbcontext);             app.createperowincontext<applicationrolemanager>(applicationrolemanager.create);             app.createperowincontext<applicationusermanager>(applicationusermanager.create);              app.usecookieauthentication(                 new cookieauthenticationoptions()                 {                     authenticationtype = defaultauthenticationtypes.applicationcookie,                     loginpath = new pathstring("/login")                 });         }          /// <summary>         /// gets instance of dbcontext owin         /// </summary>         /// <returns></returns>         private imycontext getdbcontext()         {             // create dbcontext instance             return new mycontext();         }     } 

as can see, create instance of dbcontext using new keyword, when want making use of ninject inject dbcontext in.

is possible me wire ninject inject database context in within owin?

i'm guessing best can here is:

dependencyresolver.current.getservice<imycontext>(); 

update

after discussion issue in chat established that:

  • owin app self-hosted , not reference system.web
  • it separate mvc application used ninject.mvc

this means di container can't shared between two. composition root of application should close starting point possible, which, owin, means start method.

in particular use case not gaining having ninject in application instantiating dbcontext right next composition root.

the article trailmax linked says:

also not use our unity container, uses owin object resolution. don’t want mix owin registrations unity registrations many reasons (the biggest reason lack of lifetime management between 2 containers).

so main advantage of using di container owin if using rest of application, , want owin , rest of application share registrations. that's not case.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -