在MVC 5应用程序中使用autofac注入SignalR集线器的依赖项

B S*_*arp 5 asp.net-mvc dependency-injection autofac signalr signalr-hub

我正在尝试将SignalR 2引入现有项目,在该项目中,所有依赖项注入均使用autofac执行,所有依赖项配置均在Global.asax中执行。我找到了将SignalR与autofac一起使用的Autofac.SignalR软件包及其随附的文档

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

不幸的是,当尝试从lifetimeScope解析依赖项时,我的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.
Run Code Online (Sandbox Code Playgroud)

我无法获得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;
Run Code Online (Sandbox Code Playgroud)

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

该行上的cub类中发生了异常

_userSignalRConnectionRepository = _hubLifetimeScope.Resolve<IUserSignalRConnectionRepository>();
Run Code Online (Sandbox Code Playgroud)

hal*_*r73 5

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

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

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

另外,您可以使用InstancePerDependency()(默认)或SingleInstance()代替注册IUserSignalRConnectionRepository InstancePerHttpRequest()