我很確定我錯過了一個非常基本和簡單的事情。Autofac WebApi集成與MultiTenant容器不兼容
我使用Autofac和它的如下
var builder = new ContainerBuilder();
// Registratino of modules here
var container = builder.Build();
var tenantStrategy = new AppSettingsTenantIdentifier(appSettings);
var mtc = new MultitenantContainer(tenantStrategy, container);
//這裏租戶特定模塊登記
變種解析器=新AutofacWebApiDependencyResolver(MTC)的多租戶容器;
GlobalConfiguration.Configuration.DependencyResolver = resolver;
這被稱爲Application_Start
。經過上面的叫,我嘗試註冊類解決一個如下
var webApiConfig = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof (WebApiConfig)) as WebApiConfig;
一個這個類的依賴關係被註冊爲InstancePerAPiRequest
的。在這一點上,我得到以下錯誤。
No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
我不知道我怎樣才能解決這個問題。我知道我可以用標記AutofacWebRequest
創建一個新的生命週期範圍,然後使用該生命週期範圍來解決,但這對我來說並不合適。
假設WebApiConfig是Web API的默認類,它是靜態的。爲什麼你需要通過容器解決它? –
我們必須更改該類,因爲我們爲不同的客戶端配置了不同的路由。 – Suhas