Injection dependencies for SignalR hubs using autofac in in MVC 5 application

I am trying to bring SignalR 2 to an existing project in which all dependency injection is done using autofac and all dependency configuration is done in Global.asax. I found the Autofac.SignalR package for using SignalR with autofac and its accompanying documentation .

I followed the example in the provided documentation and followed the proposal to use the RegisterHubs function, and not to determine my individual hub dependencies.

Unfortunately, my Hub class gets the following runtime error when trying to resolve lifetimeScope dependency

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.

I was not able to get DependencyResolver.Current or ILifeTimeScopeProvider to work for me.

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>();
+4
1

, IUserSignalRConnectionRepository.

, , , InstancePerHttpRequest(), InstancePerMatchingLifetimeScope("AutofacWebRequest"). MVC, SignalR (, , , ).

, , lifetimeScope.BeginLifetimeScope("AutofacWebRequest");. lifetimeScope.BeginLifetimeScope(); .

IUserSignalRConnectionRepository, InstancePerDependency() ( ) SingleInstance() InstancePerHttpRequest().

+4

Source: https://habr.com/ru/post/1530115/


All Articles