SignalR + Autofac + OWIN:为什么 GlobalHost.ConnectionManager.GetHubContext 不起作用? [英] SignalR + Autofac + OWIN: Why doesn't GlobalHost.ConnectionManager.GetHubContext work?

查看:34
本文介绍了SignalR + Autofac + OWIN:为什么 GlobalHost.ConnectionManager.GetHubContext 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个项目中使用 OWIN、SignalR 和 Autofac.

I'm trying to use OWIN, SignalR and Autofac in a single project.

我正在对 signalR 进行如下设置:

I'm setting things up with regards to signalR as follows:

       // Create the AutoFac container builder:
        var builder = new ContainerBuilder();

        // ...[Register various other things in here]...

        // register signalR Hubs
        builder.RegisterHubs(Assembly.GetExecutingAssembly());

        // Build the container:
        var container = builder.Build();

        // Configure SignalR with the dependency resolver.
        app.MapSignalR(new HubConfiguration
        {
            Resolver =  new AutofacDependencyResolver(container)
        });

我的问题是,当我使用 Autofac SignalR 集成时,我无法再在服务器上获得 signalR 集线器上下文(例如在 webapi 控制器中),因此无法从服务器端向连接的客户端发送消息.当我不使用 Autofac signalR 集成时,我是如何做到这一点的:

My issue is that when I use the Autofac SignalR integration, I can no longer get a signalR Hub Context on the server (in a webapi controller for example) and so can't send messages from server side to the connected clients. Something like the following is how I do this when I'm not using the Autofac signalR integration:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
hubContext.Clients.All.notification("Test Message");

但是当我将 Autofac 添加到组合中时,这不起作用 - 我没有收到任何错误消息,而且我似乎确实得到了一个 hubContext,但是对它的调用实际上似乎并没有到达客户端.

But this doesn't work when I add Autofac into the mix - I don't get any error message and I do seem to get a hubContext, but calls on it don't actually seem to get to the clients.

如果我在对 MapSignalR 的调用中注释掉对 signalR 的依赖解析器的使用,则对 GetHubContext 的调用再次起作用并且消息成功地到达了 signalR 客户端,但当然我不能再在我的集线器上使用 IoC.例如

If I comment out the use of the dependency resolver for signalR in the call to MapSignalR, the call to GetHubContext works again and messages reach the signalR clients sucessfully, but of course I can no longer use IoC on my hubs. e.g.

        // Configure SignalR with the dependency resolver.
        app.MapSignalR(new HubConfiguration
        {
            // Resolver =  new AutofacDependencyResolver(container)
        });

谁能告诉我为什么使用 AutofacDependencyResolver 会阻止 GlobalHost.ConnectionManager.GetHubContext 正常工作??

注意:我尝试过的另一件事不是使用 GlobalHost.ConnectionManager.GetHubContext() 我尝试将 IConnectionManager 注入 webapi 控制器,我想从中向客户端发送消息,然后调用 GetHubContext,但 Autofac无法解析 IConnectionManager.

NOTE: One other thing I have tried is instead of using GlobalHost.ConnectionManager.GetHubContext() I tried injecting an IConnectionManager into the webapi controller from which I want to send a message to clients, then calling GetHubContext on that, but Autofac couldn't resolve the IConnectionManager.

我确实找到了 Piotr Szmyd 的以下文章,它显然允许这样做:

I did find the following article by Piotr Szmyd which apparently allows this:

http://www.szmyd.com.pl/blog/wiring-signalr-with-autofac

但这似乎是基于过时的signalR构建,虽然这里似乎有一个nuget包:

but this appears to be based on obsolete signalR builds, and while there seems to be a nuget package for it here:

http://www.nuget.org/packages/SignalR.AutoFac/

它似乎也已经过时了.

推荐答案

如果你在 SignalR 中使用自定义依赖解析器,除非你修改它,否则你不能再使用 GlobalHost:

If you use a custom dependency resolver with SignalR, you can no longer use GlobalHost unless you modify it:

GlobalHost.DependencyResolver = new AutofacDependencyResolver(container);
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();

// A custom HubConfiguration is now unnecessary, since MapSignalR will
// use the resolver from GlobalHost by default.
app.MapSignalR();

如果不想修改GlobalHost,则必须手动解析您的IConnectionManager:

If you don't want to modify GlobalHost, you will have to manually resolve your IConnectionManager:

IDependencyResolver resolver = new AutofacDependencyResolver(container);
IHubContext hubContext = resolver.Resolve<IConnectionManager>().GetHubContext<MyHub>();

app.MapSignalR(new HubConfiguration
{
    Resolver = resolver
});

这篇关于SignalR + Autofac + OWIN:为什么 GlobalHost.ConnectionManager.GetHubContext 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆