我有一個多租戶MVC5 webapp,它使用Autofac v3.5.2和Autofac.Mvc5 v3.3.4。解決OWIN啓動類中的Autofac依賴關係
我的Autofac DI接線發生在我的MVC項目中的一個類中。對於身份驗證,我們使用OWIN OpenId middleware與Azure B2C進行集成。在OWIN啓動類中,我需要依賴項來使用來自當前請求的信息來設置tenantId
/clientId
。 我嘗試通過抓住依賴性:
DependencyResolver.Current.GetService<...>();
然而,這總是拋出ObjectDisposedException
實例不能得到解決,嵌套的壽命可以,因爲它已經被設置爲不從這個LifetimeScope創建。
我們在我們的應用程序中有一個ISiteContext,它有一個請求生命週期。它將獲得特定於當前請求的配置值。我試圖取這些值,如下所示:
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時發生此錯誤。我不知道我在這裏做錯了什麼。目前我沒有Autofac我的啓動Configuration(IAppBuilder app)
方法中的DI設置,因爲這已經在我的MVC項目中設置。
你能分享更多的代碼嗎?你是如何獲得'tenantId'的? –
@CyrilDurand我添加了一個代碼示例,顯示我試圖解析依賴關係以獲取每個請求的客戶端的位置 –