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

查看:1787
本文介绍了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 ??

请注意:我曾尝试的另一件事是不是使用G​​lobalHost.ConnectionManager.GetHubContext()我试图注入一个IConnectionManager到从中我想将消息发送给客户端,然后在该调用GetHubContext的控制器的WebAPI,但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:

<一个href=\"http://www.szmyd.com.pl/blog/wiring-signalr-with-autofac\">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:

<一个href=\"http://www.nuget.org/packages/SignalR.AutoFac/\">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天全站免登陆