3

I am trying to follow this excellent explantation: SignalR + Autofac + OWIN: Why doesn't GlobalHost.ConnectionManager.GetHubContext work?

But I can only get it to work if I set the Global resolver. If I try the second example, my hub never actually sends anything. Here is the service that is injected into my Web API controller:

    public ClientCommunicationService(ILifetimeScope lifetimeScope)
    {
        this._lifetimeScope = lifetimeScope;
    }

    public Task SendCreateListenerMessage(ServerListenerModel listenerModel)
    {
       IDependencyResolver resolver = new AutofacDependencyResolver(this._lifetimeScope);

       IHubContext hubContext = resolver.Resolve<IConnectionManager>().GetHubContext<RealTimeHub>();
        return hubContext.Clients.All.AddServerListener(listenerModel);

        //var hubContext = GlobalHost.ConnectionManager.GetHubContext<RealTimeHub>();
        //return hubContext.Clients.All.AddServerListener(listenerModel);
    }

and my OWIN setup:

        app.UseAutofacMiddleware(container);
        app.UseCors(CorsOptions.AllowAll);
        ConfigureOAuth(app, container);
        var webApiConfiguration = GetWebApiConfiguration();
        webApiConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        app.UseAutofacWebApi(webApiConfiguration);
        app.UseWebApi(webApiConfiguration);
        var signalRConfiguration = GetHubConfiguration();
        signalRConfiguration.Resolver = new AutofacDependencyResolver(container);
        //GlobalHost.DependencyResolver = new AutofacDependencyResolver(container);

        app.MapSignalR("/signalr", signalRConfiguration);
        app.UseStaticFiles("/FrontEnd");
        InitializeData(container);
Community
  • 1
  • 1
Ben
  • 115
  • 10

0 Answers0