解决OWIN Startup类中的Autofac依赖项 [英] Resolve Autofac dependencies in OWIN Startup class

查看:379
本文介绍了解决OWIN Startup类中的Autofac依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多租户MVC5 Web应用程序,它使用的是Autofac v3.5.2和Autofac.Mvc5 v3.3.4.

I have a multi-tenant MVC5 webapp, which is using Autofac v3.5.2 and Autofac.Mvc5 v3.3.4.

我的Autofac DI接线在我的MVC项目中的一个类中进行.对于身份验证,我们使用 OWIN OpenId中间件与Azure B2C集成.在OWIN Startup类中,我需要一个依赖项来使用当前请求中的信息来设置tenantId/clientId. 我尝试通过:

My Autofac DI wiring takes place in a class within my MVC project. For authentication, we are using OWIN OpenId middleware to integrate with Azure B2C. In the OWIN Startup class I need a dependency to set tenantId/clientId using information from the current request. I try to grab the dependency via :

DependencyResolver.Current.GetService<...>(); 

但是,这总是抛出ObjectDisposedException

无法解析实例,并且无法从此LifetimeScope创建嵌套生命周期,因为该LifetimeScope已被处置.

Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.

我们的应用程序中有一个具有请求生存期的ISiteContext.它填充有特定于当前请求的配置值.我试图像这样获取这些值:

We have an ISiteContext in our application that has a request-lifetime. It gets populated with configuration values specific to the current request. I am trying to fetch these values like this:

private OpenIdConnectAuthenticationOptions CreateOptionsFromPolicy(string policy)
{
    var options = new OpenIdConnectAuthenticationOptions
        {
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                ...
                RedirectToIdentityProvider = SetSettingsForRequest
            }
        }
}

private Task SetSettingsForRequest(RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
{
    var siteContext = DependencyResolver.Current.GetService<ISiteContext>();
    context.ProtocolMessage.ClientId = siteContext.ClientId;
    return Task.FromResult(0);
}

尝试在SetSettingsForRequest中使用DependencyResolver时发生错误.我不知道我在这里做错了什么.目前,我的Startup Configuration(IAppBuilder app)方法中没有 Autofac DI设置,因为这已经在我的MVC项目中设置了.

The error happens when trying to use the DependencyResolver in SetSettingsForRequest. I have no clue as to what I am doing wrong here. Currently I have no Autofac DI setup in my Startup Configuration(IAppBuilder app) method, since this is already setup in my MVC project.

推荐答案

正如MickaëlDerriey指出的那样,以下代码是一种能够解决OWIN中请求范围的依赖关系的解决方案:

As Mickaël Derriey pointed out, the following code is a solution to being able to resolve request-scoped dependencies in OWIN:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        // Register dependencies, then...
        var container = builder.Build();

        // Register the Autofac middleware FIRST. This also adds
        // Autofac-injected middleware registered with the container.
        app.UseAutofacMiddleware(container);

        // ...then register your other middleware not registered
        // with Autofac.
    }
}

,然后在以后的任何代码中使用它来解决依赖关系:

and then use in any later code to resolve a dependency:

// getting the OWIN context from the OWIN context parameter
var owinContext = context.OwinContext;
var lifetimeScope = owinContext.GetAutofacLifetimeScope(); 
var siteContext = lifetimeScope.GetService<ISiteContext>();
context.ProtocolMessage.ClientId = siteContext.ClientId;

使用此解决方案,我再也没有任何问题可以解决请求范围的依赖关系,因为Autofac似乎符合OWIN创建/处置请求范围的方式.

Using this solution, I did not have any issues anymore resolving request-scoped dependencies, since Autofac seems to accord to the OWIN way of creating/disposing of a request scope.

这篇关于解决OWIN Startup类中的Autofac依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆