的WebAPI +简单的喷油器+ OWIN [英] WebApi + Simple Injector + OWIN
问题描述
我想在的WebAPI项目中使用SimpleInjector与OWIN。然而,在下面的行 ConfigureAuth
失败
I am trying to use SimpleInjector with OWIN in a WebAPI project. However, the following line in ConfigureAuth
fails
app.CreatePerOwinContext(container.GetInstance<ApplicationUserManager>);
该异常的的ApplicationUserManager注册为网络API请求的生活方式,但实例被请求的Web API请求上下文之外的
我使用 container.RegisterWebApiRequest< ApplicationUserManager>();
在容器初始化。 (不会有任何异常,如果我使用注册
而不是 RegisterWebApiRequest
但这不是按首选方法简单的注射器文档)
I am using 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)
据我了解, ApplicationUserManager
需要使用 CreatePerOwinContext
为OWIN正常工作进行注册。我不知道我们该怎么做因为启动时喷油简单无法解决情况下,这种使用简单注射器。
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.
我已经尝试在方法本SO回答,但它失败了同样的信息。
I have already tried the method in this SO answer but it fails with the same message.
任何想法,我怎么能解决这个问题?
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();
}
});
}
然后叫 app.UseOwinContextInjector(容器);
注册依赖关系之后。
感谢这个帖子
这篇关于的WebAPI +简单的喷油器+ OWIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!