13

I am trying to use SimpleInjector with OWIN in a WebAPI project. However, the following line in ConfigureAuth fails

app.CreatePerOwinContext(container.GetInstance<ApplicationUserManager>);

The exception is The ApplicationUserManager is registered as 'Web API Request' lifestyle, but the instance is requested outside the context of a Web API Request.

I am using container.RegisterWebApiRequest<ApplicationUserManager>(); in container initialization. (there won't be any exceptions if I use Register instead of RegisterWebApiRequestbut this is not the preferred method as per simple injector docs)

As I understand, ApplicationUserManager needs to be registered using CreatePerOwinContext for OWIN to work properly. I wonder how do we do this using Simple Injector given that Simple Injector cannot resolve instances during startup.

I have already tried the method in this SO answer but it fails with the same message.

Any idea how can I resolve this?

Community
  • 1
  • 1
su8898
  • 1,703
  • 19
  • 23

2 Answers2

13

I used the following code to solve this issue.

public static void UseOwinContextInjector(this IAppBuilder app, Container container)
{
// Create an OWIN middleware to create an execution context scope
app.Use(async (context, next) =>
{
     using (var scope = container.BeginExecutionContextScope())
     {
         await next.Invoke();
     }
});
}

and then called app.UseOwinContextInjector(container); right after registering the dependancies.

Thanks to this post

su8898
  • 1,703
  • 19
  • 23
  • 4
    Do you have the full example somewhere to look at, I am trying do something very similar, but I am also using IdentityServer3, a handler an attribute for versioning. – OutOFTouch Sep 10 '15 at 21:46
  • Can you provide a full example on Owin + MVC + SimpleInjector ? – Jerome2606 Apr 14 '16 at 15:14
2

You may find this question useful. The idea is to avoid using OWIN to resolve dependencies because it introduces some clutter to your controllers code. The following code that uses OWIN to resolve UserManager instance is a Service Locator anti-pattern:

public ApplicationUserManager UserManager
{
    get
    {
        return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();                
    }
    set
    {
        _userManager = value;
    }
}

Instead of relying on OWIN to resolve dependencies, inject required services into your controller's constructor and use IDependencyResolver to build controller for you. This article demonstrates how to use dependency injection in ASP.NET Web API.

Community
  • 1
  • 1
Sergey Kolodiy
  • 5,829
  • 1
  • 36
  • 58
  • 1
    Thanks. I am not using OWIN to resolve dependencies. OWIN internally uses ApplicationUserManager and tries to get this from OwinContext. So making OWIN aware of the ApplicationUserManager instance is mandatory for OWIN to work(please correct me if I am wrong). I am not using the code you quoted in the AccountController. AccountController is getting ApplicationUserManager via constructor injection. So the problem remains. – su8898 Dec 17 '14 at 10:16
  • 1
    @su8898 then I could recommend you to have a look at [this blog post](http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/) — maybe you'll find some useful code samples there. – Sergey Kolodiy Dec 17 '14 at 10:33
  • the examples in the blog post use `app.CreatePerOwinContext(() => DependencyResolver.Current.GetService());` in _ConfigureAuth_ because it's an MVC project. WebAPI has a different implementation for the DependancyResolver. Even if we use a method like this, I think it would fail because SimpleInjector won't be able to resolve `ApplicationUserManager` in _ConfigureAuth_. I am wondering how do I go around this issue! – su8898 Dec 17 '14 at 10:58