gpt4 book ai didi

asp.net - Autofac DependencyResolutionException for ILifetimeScope for a hub in asp.net mvc, signalr, MS owin

转载 作者:行者123 更新时间:2023-12-01 09:55:58 24 4
gpt4 key购买 nike

Asp.net Mvc 5.2、SignalR 2.1、MS Owin (Katana) 3.0 的 Autofac 3.5 配置应该是什么?是否有更简单的方法来注册 Autofac 解析器(现在有两个)?或者为什么 ILifetimeScope 对于我的集线器不可见?

异常:

Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Autofac.ILifetimeScope)' on type 'OperatorHub'. --->

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. (See inner exception for details.) --->

Autofac.Core.DependencyResolutionException: 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.

在我的 OwinStartup 中(参见 autofac + mvc owinautofac + signalr in owin):

public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();

// ... registration. There is .InstancePerRequest() and .SingleInstance()

Autofac.Integration.Mvc.RegistrationExtensions.RegisterControllers(builder,typeof(MvcApplication).Assembly);
Autofac.Integration.SignalR.RegistrationExtensions.RegisterHubs(builder, Assembly.GetExecutingAssembly());

var container = builder.Build();

// 1st resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

app.UseAutofacMiddleware(container);
app.UseAutofacMvc();

// yet the 2nd resolver!
app.MapSignalR(new HubConfiguration { Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container) });
}

枢纽:

public class OperatorHub : Hub
{
public OperatorHub(ILifetimeScope hubLifetimeScope)
{
hubLifetimeScope.BeginLifetimeScope();
// ...

// HERE IT FALLS. The IMyService relates to MyDbContext (see below)
var myservice = hubLifetimeScope.Resolve<IMyService>();

}
}

更新

中断组件注册(EF 上下文:

        builder.RegisterType<MyDbContext>().AsSelf().As<DbContext>().InstancePerRequest("OwinLifetimeScope");

简而言之,错误是 MyDbContext 不在传递给 OperatorHub 构造函数的“根”生命周期范围内。

更新 2

在@TravisIllig 的帮助下,解决方案是使用 .InstancePerLifetimeScope() 注册 MyDbContext 服务,并在中心创建一个。将为 asp mvc 中的 http 请求创建另一个生命周期范围。在 Sharing Dependencies Across Apps Without Requests 创建帮助.

此外,集线器不应处理给定范围,因为它是导致第二次运行时 ObjectDisposedException 的根范围。

最佳答案

There is an FAQ on handling this exact exception on the Autofac doc site.问题源于您将 InstancePerRequest 与 SignalR 结合使用,also per the documentation :

Due to SignalR internals, there is no support in SignalR for per-request lifetime dependencies.

您确实看过 the Autofac SignalR docs如我所见,您已经注入(inject)了一个生命周期范围来帮助您管理实例生命周期,但这并没有为您提供每个请求的生命周期范围,它只是为您提供了一个中心生命周期范围。我可能会建议重新访问该文档以进行复习。

The FAQ I mentioned ,连同 SignalR 集成文档,应该可以为您的应用指明正确的解决方案。许多人只是将他们的注册从 InstancePerRequest 切换到 InstancePerLifetimeScope,但我强烈建议您在做出该决定之前阅读常见问题解答并检查您的选项。这可能是正确的选择,但也可能不是 - 这取决于您的应用在内部的工作方式。

关于asp.net - Autofac DependencyResolutionException for ILifetimeScope for a hub in asp.net mvc, signalr, MS owin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27603989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com