简体   繁体   English

WebApi + Simple Injector + OWIN

[英]WebApi + Simple Injector + OWIN

I am trying to use SimpleInjector with OWIN in a WebAPI project. 我试图在WebAPI项目中使用带有OWIN的SimpleInjector。 However, the following line in ConfigureAuth fails 但是, ConfigureAuth的以下行失败

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. 例外情况是ApplicationUserManager注册为“Web API请求”生活方式,但实例是在Web API请求的上下文之外请求的。

I am using container.RegisterWebApiRequest<ApplicationUserManager>(); 我正在使用container.RegisterWebApiRequest<ApplicationUserManager>(); in container initialization. 在容器初始化。 (there won't be any exceptions if I use Register instead of RegisterWebApiRequest but this is not the preferred method as per simple injector docs ) (如果我使用Register而不是RegisterWebApiRequest则不会有任何例外,但根据简单的注入器文档,这不是首选方法)

As I understand, ApplicationUserManager needs to be registered using CreatePerOwinContext for OWIN to work properly. 据我所知,需要使用CreatePerOwinContext注册ApplicationUserManager ,以便OWIN正常工作。 I wonder how do we do this using Simple Injector given that Simple Injector cannot resolve instances during startup. 我想知道我们如何使用Simple Injector这样做,因为Simple Injector在启动期间无法解析实例。

I have already tried the method in this SO answer but it fails with the same message. 我已经在这个SO答案中尝试了这个方法,但它失败了同样的消息。

Any idea how can I resolve this? 知道我怎么解决这个问题?

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); 然后调用app.UseOwinContextInjector(container); right after registering the dependancies. 在注册了依赖项之后。

Thanks to this post 感谢这篇文章

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. 我们的想法是避免使用OWIN来解决依赖关系,因为它会给控制器代码带来一些混乱。 The following code that uses OWIN to resolve UserManager instance is a Service Locator anti-pattern : 以下使用OWIN解析UserManager实例的代码是Service Locator反模式

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. 而不是依赖OWIN来解决依赖关系,将所需的服务注入到控制器的构造函数中,并使用IDependencyResolver为您构建控制器。 This article demonstrates how to use dependency injection in ASP.NET Web API. 本文演示如何在ASP.NET Web API中使用依赖项注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM