SignalR: Sending data using GlobalHost.ConnectionM

2019-01-11 23:46发布

I have a hub like this:

public class MessageHubBub : Hub
{

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

My (relavent) javascript looks like this:

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

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

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.

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?

EDIT: 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/

The only line of configuration I have is as follows:

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

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 );
        }

    }

标签: c# signalr
1条回答
Rolldiameter
2楼-- · 2019-01-12 00:08

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>();

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;

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

查看更多
登录 后发表回答