Asp.net web api Autofac WebApi集成不适用于多租户容器

Asp.net web api Autofac WebApi集成不适用于多租户容器,asp.net-web-api,autofac,Asp.net Web Api,Autofac,我很确定我在这里遗漏了一件非常基本和简单的事情 我使用的是Autofac及其多租户容器,如下所示 var builder = new ContainerBuilder(); // Registratino of modules here var container = builder.Build(); var tenantStrategy = new AppSettingsTenantIdentifier(appSettings); var mtc = new MultitenantConta

我很确定我在这里遗漏了一件非常基本和简单的事情

我使用的是Autofac及其多租户容器,如下所示

var builder = new ContainerBuilder();
// Registratino of modules here
var container = builder.Build();

var tenantStrategy = new AppSettingsTenantIdentifier(appSettings);
var mtc = new MultitenantContainer(tenantStrategy, container);
//在此处注册特定于租户的模块

var resolver=新的AutofacWebApidenceResolver(mtc)

GlobalConfiguration.Configuration.DependencyResolver=解析程序

这是从
应用程序\u Start
调用的。调用上面的函数后,我尝试解析其中一个注册的类,如下所示

var webApiConfig = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof (WebApiConfig)) as WebApiConfig;
此类的一个依赖项注册为
InstancePerapRequest
。在这一点上,我得到以下错误

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
,然后使用该生存期范围进行解析,但这对我来说并不合适。

如果您将某个内容注册为
InstancePerRequest
InstancePerApiRequest
,则无法在请求之外进行解析

如果需要在请求生存期(实际API请求之外)之外解析依赖项,请为其选择不同的生存期
InstancePerLifetimeScope
InstancePerApiRequest
非常接近,并且也将基于每个请求工作


但要小心:如果您解决了来自容器的请求之外的某个问题,并且该问题是
IDisposable
,那么该对象将在应用程序的整个生命周期内一直存在,因为Autofac会抓住它并尝试为您处理它。如果您不知道该行为,这可能是一个缓慢的内存泄漏

假设WebApiConfig是WebAPI中的默认类,并且是静态的。为什么需要通过容器解决它?我们必须更改该类,因为我们为不同的客户端配置了不同的路由。链接更新:关于确定性处理