Question

I apologize for the long question (I have to paint the full picture).

I have a SignalR Hub that I'm injecting in one of my services, here is the service:

private readonly INotificationRepository _notificationRepository;
private readonly INotificationHub _notificationHub;
private readonly IUserRepository _userRepository;

public NotificationService(INotificationRepository notificationRepository,
                           INotificationHub notificationHub,
                           IUserRepository userRepository)
{
    _notificationRepository = notificationRepository;
    _notificationHub = notificationHub;
    _userRepository = userRepository;
}

This is how my NotificationHub looks like:

[Authorize]
public class NotificationHub : Hub, INotificationHub
{
    // other stuff here 

    // works - no issues -> Context.User.Identity.Name has a value as expected
    public override Task OnConnected()
    {
        string name = Context.User.Identity.Name;
        // do stuff with the name ...
        return base.OnConnected();
    }

    // at random intervals is being trigerred 
    // and Context.User.Identity throws a "ObjectDisposedException"
    public override Task OnDisconnected()
    {
        string name = Context.User.Identity.Name;
        // do some more stuff with the name ...
        return base.OnDisconnected();
    }
}

This is how I'm wiring the IoC (Autofac in this case):

builder.RegisterType<NotificationService>().As<INotificationService>().InstancePerHttpRequest();
builder.RegisterType<NotificationRepository>().As<INotificationRepository>().InstancePerHttpRequest();
builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerHttpRequest();
builder.RegisterType<NotificationHub>().As<INotificationHub>().ExternallyOwned();

I've been stuck here for hours, I have a feeling that I'm not wiring my dependencies correct (in terms of the lifetime scope). However I can't figure it out.

EDIT

I've just tested with Chrome and it seems to be working - the exception is only raised in IE (in my case I've tested with IE11).

Also I'm using Windows Authentication (tried it on another project with Forms Authentication and it seems to be working).

Not sure if a bug.

Was it helpful?

Solution

I've tried a lot of things and I managed to fix this by adding the following on the client side:

window.onbeforeunload = function (e) {
    $.connection.hub.stop();
};

I believe this is a bug in the version of SignalR that I'm using (2.0.3). I've logged it here: https://github.com/SignalR/SignalR/issues/2972

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top