Question

I've been using SignalR for a while in my MVC5 project, but only in the recent weeks I encountered this problem.

Every time I refresh a page or navigate to a different page within my project, it takes a long time (between 3-8 seconds) to establish new connection.

[09:18:12 GMT+0100 (GMT Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:53516/signalr/connect?transport=serverSentEvents&connectio…3A%22notehub%22%7D%2C%7B%22name%22%3A%22retailpriceindexhub%22%7D%5D&tid=6'. jquery.signalR-2.0.3.min.js:8
[09:18:18 GMT+0100 (GMT Daylight Time)] SignalR: EventSource connected.

Although normally I don't use them, I created those override methods for one of my hubs on the server just to see when breakpoints are being hit:

    public override Task OnConnected()
    {
        var connectionId = Context.ConnectionId;
        return base.OnConnected();
    }

    public override Task OnReconnected()
    {
        var connectionId = Context.ConnectionId;
        return base.OnReconnected();
    }

    public override Task OnDisconnected()
    {
        var connectionId = Context.ConnectionId;
        return base.OnDisconnected();
    }

This confirmed that on page refresh OnDisconnected method doesn't get called immediately, but after the delay mentioned above. Once it gets hit, it's followed by OnConnected straight away and everything goes smoothly from that point.

Initially I thought it was similar to: https://github.com/SignalR/SignalR/issues/2719, but:

  • that was an issue with Chrome (Version 31.0.1650.57); I am on Version 34.0.1847.116 m.
  • my issue affects all browsers (IE being slightly quicker than Chrome or Firefox)
  • according to that issue OnDisconnected was delayed only when navigating to another page in the project - in my case OnDisconnected is not called immediately even when I close the tab or navigate to an external page

When I open a new tab, connection is established immedietaly:

[09:31:26 GMT+0100 (GMT Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:53516/signalr/connect?transport=serverSentEvents&connectio…3A%22notehub%22%7D%2C%7B%22name%22%3A%22retailpriceindexhub%22%7D%5D&tid=6'. jquery.signalR-2.0.3.min.js:8
[09:31:26 GMT+0100 (GMT Daylight Time)] SignalR: EventSource connected. 

This proves to me that the delay happens somewhere between $.connection.hub.start() and hitting OnDisconnected method on the server, but I don't know how to trace it down.

I have enabled server side logging by modifying Web.config file, but I can't see anything obvious in the logs.

Also, I have tried changing the transport method to longPolling, but the issue still exists.

Similar issued raised on SignalR github page mentioned AVG or proxy, but none of these are relevant in my case.

I have updated signalR packages (server and client) as well as NewtonSoft.Json to the latest versions to no avail.

Was it helpful?

Solution

The issue has been resolved by changing Ninject scoping for my DbFactory and UnitOfWork bindings. I didn't include that part of code in my question, as I had no idea that's where the issue was.

Originally I was using InRequestScope. After changing to InThreadScope, the issue with slow connection disappeared, however I run into more problems then with DbContext lifecycles.

InCallScope from Ninject.Extensions.NamedScope seems to be ideal for my needs.

Bind<IDbFactory>().To<DbFactory>().InCallScope();
Bind<IUnitOfWork>().To<UnitOfWork>().InCallScope();

If someone can explain why InRequestScope or default InTransientScope had such impact on SignalR performance (initial connection) in my project, I will be happy to accept it as an answer.

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