本文介绍了SignalR:使用GlobalHost.ConnectionManager发送数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的中心:

public class MessageHubBub : Hub
{

    public void ServerMethod()
    {
        Clients.All.sayHi("hello");
        GlobalHost.ConnectionManager.GetHubContext<MessageHubBub>().Clients.All.sayHi( "Hello" );
    }
}

我的(相关的)JavaScript看起来像这样:

My (relavent) javascript looks like this:

 $.connection.MessageHubBub.client.sayHi = function (message) {
                console.log("Hello");
            };

            $.connection.hub.start().done(function () {
                $.connection.MessageHubBub.server.ServerMethod();
            });

真正奇怪的是, Hello仅打印一次,而我原本希望这样被打印两次(因为 sayHello被调用了两次)。通常,我一直在使用从GlobalHost.ConnectionMananager获取的客户端对象向客户端发送消息时遇到麻烦,因此我将这个问题简化为无法解决的问题。

The really strange thing is that "Hello" is being printed only once, where I would have expected it to be printed twice (since 'sayHello' is called twice). In general, I've been running into trouble with using the 'clients' object obtained from the GlobalHost.ConnectionMananager to send messages to clients, so I've distilled this problem down to show what doesn't work.

我见过很多帖子,这些帖子中的人有一些问题,例如在启动中心之前未与客户端重新注册其js处理程序或未引入正确的js依赖项,但这些似乎没有是我的问题。有什么原因使我无法使用GlobalHost.ConnectionManager.GetHubContext()。Clients向客户端发送消息?

I've seen lots of posts with people having issues such as not regisitering their js handler with the client before starting the hub or not bringing in the correct js dependencies, but these don't seem to be my issue. Is there any reason why I wouldn't be able to send messages to the client using GlobalHost.ConnectionManager.GetHubContext().Clients?

编辑:

In response to Lars, I do have a custom dependency resolver so that I can integrate Unity into SignalR. I followed an example I found here: http://www.kevgriffin.com/using-unity-for-dependency-injection-with-signalr/

我仅有的配置行如下:

RouteTable.Routes.MapHubs( new HubConfiguration() { Resolver = new SignalRUnityDependencyResolver( unityContainer ) } );

SignalRUnityDependencyResolver看起来像这样:

The SignalRUnityDependencyResolver looks like this:

public class SignalRUnityDependencyResolver : DefaultDependencyResolver
    {
        private IUnityContainer _container;

        public SignalRUnityDependencyResolver( IUnityContainer container )
        {
            _container = container;
        }

        public override object GetService( Type serviceType )
        {
            if ( _container.IsRegistered( serviceType ) ) return _container.Resolve( serviceType );
            else return base.GetService( serviceType );
        }

        public override IEnumerable<object> GetServices( Type serviceType )
        {
            if ( _container.IsRegistered( serviceType ) ) return _container.ResolveAll( serviceType );
            else return base.GetServices( serviceType );
        }

    }


推荐答案

使用自定义依赖性解析器时,不足以将其传递给HubConfiguration。

When using a custom dependency resolver, it is not enough to pass it to the HubConfiguration.

您需要将解析器实例存储在某个位置,并使用它来访问连接管理器和集线器上下文:

You need to either store the resolver instance somewhere and use it like this to access the connection manager and your hub context:

MyDependencyResolver.Resolve<IConnectionManager>().GetHubContext<MyHub>();

或将GlobalHost中的默认依赖项解析器设置为您的实例:

or set the default dependency resolver in GlobalHost to your instance:

var myResolver = new SignalRUnityDependencyResolver(unityContainer);
RouteTable.Routes.MapHubs( new HubConfiguration() { Resolver = myResolver } );
GlobalHost.DependencyResolver = myResolver;

(然后您可以使用默认的 GlobalHost.ConnectionManager.GetHubContext< MessageHubBub> ()

(then you can use the default GlobalHost.ConnectionManager.GetHubContext<MessageHubBub>())

这篇关于SignalR:使用GlobalHost.ConnectionManager发送数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 02:25