Trying to use Simple Injector in ASP.NET MVC site with OWIN

634 views Asked by At

Reading the docs here ..it seemed easy thing to do, but in practice things are not so simple.

One problem I had was accessing the IAppBuilder interface from the method that sets up the DI. I have put an AppBuilder public static property on Startup class for that purpose and hoped that it gets initialized first, before DI setup starts:

// DI setup code:
Startup.AppBuilder.Use((async (context, next) =>
{
    using (container.BeginExecutionContextScope())
    {
        await next();
    }
}));


[assembly: OwinStartupAttribute(typeof(MySite.Presentation.Startup))]
namespace MySite.Presentation
{
    public partial class Startup
    {
        public static IAppBuilder AppBuilder { get; private set; }

        public void Configuration(IAppBuilder app)
        {
            AppBuilder = app;

            ConfigureAuth(app);
        }
    }
}

But it turns out that it's not. Startup.AppBuilder is null when the DI gets setup. How can I solve this?

I don't know if I can somehow get the container from within the Startup class but keeping the DI setup code in one place would be nice, if possible.

1

There are 1 answers

0
Steven On BEST ANSWER

Do this:

public void Configuration(IAppBuilder app)
{
    app.Use((async (context, next) =>
    {
        using (container.BeginExecutionContextScope())
        {
            await next();
        }
    }));

    ConfigureAuth(app);
}