Ich habe eine NotificationHub-Klasse, die von der Hub-Klasse erbt.Hub.Clients.User (userId) .MethodCall löst immer Objektreferenz, die nicht auf eine Instanz eines Objekts festgelegt ist
public class NotificationHub : Hub
{
public void Send(string userId, Notification notification)
{
Clients.User(userId)
.notificationReceived(notification);
}
}
Dies nicht immer mit
[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
Allerdings, wenn ich dies tun:
public class NotificationHub : Hub
{
public void Send(string userId, Notification notification)
{
var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.User(userId)
.notificationReceived(notification);
}
}
Es funktioniert .... was hier gibt? Bei den meisten Beispielen, die ich gesehen habe, ist es nicht erforderlich, den Kontext explizit anzugeben, falls er nicht bereits von Hub aus verfügbar ist. Ich hätte es lieber nicht jedes Mal explizit greifen müssen.
Hier mein IoC-Setup ist:
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);
}
}
Die 'NullReferenceException', die Sie bekommen, ist es, wenn Sie' Send() 'Methode vom Client oder von anderem Servercode aufrufen? –
Es ist beim Aufruf von Senden vom Server, Clients.User (userId) .notificationReceived (Benachrichtigung); löst die Ausnahme aus – Jack