0

I'd like to use Ninject to inject my DbContext ASP.NET Identity Framework.

At the moment I have the following line in my web.config file:

<appSettings>
    <add key="owin:appStartup" value="OwinTest.Identity.OwinStart, OwinTest.Identity" />
</appSettings>

This causes the Configuration() method on class OwinStart in my OwinTest.Identity project to be called.

Here's my 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 an instance of the DbContext for OWIN
        /// </summary>
        /// <returns></returns>
        private IMyContext GetDbContext()
        {
            // Create dbcontext instance
            return new MyContext();
        }
    }

As you can see, I create an instance of my DbContext using the new keyword, when I really want to be making use of Ninject to inject my DbContext in.

Is it possible for me to wire up Ninject to inject my database context in within OWIN?

2
  • 1
    Not Ninject discussion, but DI registrations for Identity: tech.trailmax.info/2014/09/… Commented Apr 22, 2015 at 21:31
  • @trailmax Thanks, I happened to stumble upon that article before writing this question. That example still relies on a static method that news up a DbContext. Commented Apr 23, 2015 at 9:42

1 Answer 1

1

I'm guessing the best you can do here is:

DependencyResolver.Current.GetService<IMyContext>();

Update

After discussion the issue in chat we established that:

  • Owin app is self-hosted and does not reference System.Web
  • It is separate from the MVC application that used Ninject.MVC

This basically means that DI container can't be shared between the two. The composition root of an application should be as close to the starting point as possible, which, for OWIN, means the start up method.

In this particular use case you are not gaining anything from having ninject in this application as you are instantiating your DbContext right next to your composition root.

The article trailmax linked says:

Also this does not use our Unity container, this uses Owin for object resolution. We don’t want to mix Owin registrations with Unity registrations for many reasons (the biggest reason is lack of lifetime management between 2 containers).

So the main advantage of using a DI container with owin would be if you are already using it for the rest of the application, and want owin and the rest of the application share the registrations. That's not your case.

Sign up to request clarification or add additional context in comments.

9 Comments

DependencyResolver is in the System.Web.Mvc namespace... OWIN is designed to work without any references to System.Web.
If you are not using MVC, I don't understand what your problem is. You are right at the composition root!
Do you mean the problem with newing up a DbContext without using an IoC?
Let me rephrase. Where is the composition root in your application?
My project that is making use of ASP.NET Identity Framework is indeed an MVC application, but the OWIN stuff hosts itself independently of the MVC project or the System.Web pipeline. So for the purposes of OWIN, I guess that the entry point (if that's what you mean by composition root :S) is the OwinStart class Configuration method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.