asp.net-mvc - 在 MVC 5 应用程序中使用 autofac 注入(inject) SignalR Hub 的依赖项

标签 asp.net-mvc dependency-injection signalr autofac signalr-hub

我正在尝试将 SignalR 2 引入现有项目,其中所有依赖项注入(inject)均使用 autofac 执行,所有依赖项配置均在 Global.asax 中执行。我找到了 Autofac.SignalR 包,用于将 SignalR 与 autofac 一起使用及其 accompanying documentation .

我按照提供的文档中的示例进行操作,并遵循使用 RegisterHubs 函数的建议,而不是定义我个人的集线器依赖项。

不幸的是,我的 Hub 类在尝试解析生命周期范围的依赖项时出现以下运行时错误

Autofac.Core.DependencyResolutionException was unhandled by user code
HResult=-2146233088
Message=No scope with a Tag matching 'AutofacWebRequest' is
visible from the scope in which 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.

我无法让 DependencyResolver.Current 或 ILifeTimeScopeProvider 为我工作。

我的依赖配置如下

var builder = new ContainerBuilder();
    .RegisterControllers(typeof (MvcApplication).Assembly);
    .RegisterHubs(Assembly.GetExecutingAssembly());
    ...
var container = builder.Build();

// Set dependency resolver for MVC
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

// Set dependency resolver for Web API
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

// Set the dependency resolver for SignalR
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

var signalRDependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
GlobalHost.DependencyResolver = signalRDependencyResolver;

我还根据示例设置了我的集线器类:

public class BaseHub : Hub
{
    protected readonly ILifetimeScope _hubLifetimeScope;
    private static IUserSignalRConnectionRepository _userSignalRConnectionRepository;

    public BaseHub(ILifetimeScope lifetimeScope) : base()
    {
        _hubLifetimeScope = lifetimeScope.BeginLifetimeScope();

        _userSignalRConnectionRepository = _hubLifetimeScope.Resolve<IUserSignalRConnectionRepository>();
    }

    protected override void Dispose(bool disposing)
    {
        // Dipose the hub lifetime scope when the hub is disposed.
        if (disposing && _hubLifetimeScope != null)
            _hubLifetimeScope.Dispose();

        base.Dispose(disposing);
    }
}

异常发生在线上的cub类

_userSignalRConnectionRepository = _hubLifetimeScope.Resolve<IUserSignalRConnectionRepository>();

最佳答案

您应该包含注册 IUserSignalRConnectionRepository 的代码。

错误消息似乎表明此依赖项是使用 InstancePerHttpRequest() 注册的,这相当于 InstancePerMatchingLifetimeScope("AutofacWebRequest")。对于 MVC 请求,会自动为您创建此范围,但对于 SignalR 请求则不会(这可能是一件好事,因为它们可以无限期地持续)。

您可以通过调用 lifetimeScope.BeginLifetimeScope("AutofacWebRequest"); 来解决此问题。而不是 Hub 构造函数中的 lifetimeScope.BeginLifetimeScope();

或者,您可以使用 InstancePerDependency()(默认设置)或 SingleInstance() 而不是 InstancePerHttpRequest() 来注册 IUserSignalRConnectionRepository。

关于asp.net-mvc - 在 MVC 5 应用程序中使用 autofac 注入(inject) SignalR Hub 的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184537/

相关文章:

css - MVC 中的 DropDownList 在 IE8 中不起作用

asp.net-mvc - 可访问性不一致 : property type in DbContext

c# - AWS Lambda 环境变量和依赖注入(inject)

java - Dagger 2 组件依赖

signalr - 重新连接断开连接的 SignalR 客户端 (JS) 的最佳实践

asp.net-mvc - 实现 MVC 5 IAuthenticationFilter

asp.net-mvc - 为 ASP.NET MVC 寻找网格

java - 如何在使用 GIN 注入(inject)期间传播值

c# - 没有 MVC 的 SignalR?

c# - 连接到集线器的 SignalR 2.2.0 WebSocket 错误