Hub.Clients.User(userId).methodCall始终抛出未将对象引用设置为对象的实例

Jac*_*ack 7 signalr asp.net-mvc-5

我有一个继承自Hub类的NotificationHub类.

public class NotificationHub : Hub
    {
        public void Send(string userId, Notification notification)
        {
            Clients.User(userId)
                .notificationReceived(notification);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这总是失败的

[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
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做:

public class NotificationHub : Hub
    {
        public void Send(string userId, Notification notification)
        {
            var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();

            context.Clients.User(userId)
                .notificationReceived(notification);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它有效....这里给出了什么?我见过的大多数例子都不需要明确地获取上下文,如果Hub尚未提供它?我宁愿不必每次都明确地抓住它.

这是我的IoC设置:

GlobalHost.DependencyResolver.Register(typeof(IHubActivator), () => new SimpleInjectorHubActivator(container));
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new SignalRHubUserIdProvider());
Run Code Online (Sandbox Code Playgroud)

激活:

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mic*_*evý 8

如果要从集线器处理程序方法之外向客户端发送内容(即不在服务器上处理消息期间),则必须使用 GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();

原因是当调用方法来处理某些客户端消息时,中心实例由SignalR创建,并且Clients属性已正确初始化.当您从服务器代码自己调用方法(并且可能自己创建集线器实例)时,情况并非如此.

Imho错误消息不是很清楚,SignalR应该更好地处理这个用例.无论如何,出于同样的原因,我建议将发送消息的所有方法分离到客户端,这些客户端旨在从服务器代码调用到不同的类(不是派生自Hub).