Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

c# - SignalR: Sending data using GlobalHost.ConnectionManager not working

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

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...