I have a NotificationHub class that inherits from the Hub class.
public class NotificationHub : Hub { public void Send(string userId, Notification notification) { Clients.User(userId) .notificationReceived(notification); } }
It always fails with
[NullReferenceException: Object reference not set to an instance of an object.] Microsoft.AspNet.SignalR.Hubs.SignalProxy.Invoke(String method, Object[] args) +88 Microsoft.AspNet.SignalR.Hubs.SignalProxy.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result) +12 CallSite.Target(Closure , CallSite , Object , <>f__AnonymousType0`4 ) +351
However, if I do this:
public class NotificationHub : Hub { public void Send(string userId, Notification notification) { var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(); context.Clients.User(userId) .notificationReceived(notification); } }
It works ... What gives here? Most of the examples that I saw do not require explicitly getting the context if it is not already accessible from the Hub? I would rather not take it explicitly every time.
Here is my IoC setup:
GlobalHost.DependencyResolver.Register(typeof(IHubActivator), () => new SimpleInjectorHubActivator(container)); GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new SignalRHubUserIdProvider());
Activator:
public class SimpleInjectorHubActivator : IHubActivator { private readonly Container _container; public SimpleInjectorHubActivator(Container container) { _container = container; } public IHub Create(HubDescriptor descriptor) { return (IHub) _container.GetInstance(descriptor.HubType); } }
source share