Ho una classe NotificationHub che eredita dalla classe Hub.Hub.Clients.User (userId) .methodCall genera sempre il riferimento all'oggetto non impostato su un'istanza di un oggetto
public class NotificationHub : Hub
{
public void Send(string userId, Notification notification)
{
Clients.User(userId)
.notificationReceived(notification);
}
}
questo viene a mancare sempre con
[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
Tuttavia, se faccio questo:
public class NotificationHub : Hub
{
public void Send(string userId, Notification notification)
{
var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.User(userId)
.notificationReceived(notification);
}
}
Funziona .... ciò che dà qui? La maggior parte degli esempi che ho visto non richiedono di ottenere esplicitamente il contesto, se non fosse già disponibile da Hub? Preferirei non doverlo afferrare esplicitamente ogni volta.
Ecco il mio programma di installazione CIO:
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);
}
}
La 'NullReferenceException' che si ottiene, è quando si chiama il metodo' Send() 'dal client o da un diverso codice server? –
È quando si chiama send dal server, Clients.User (userId) .notificationReceived (notifica); lancia l'eccezione – Jack