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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…