4

I'm in the process of troubleshooting an App Service that is using websockets. It's running on service plan Basic which allows for 350 websockets. This is the only app on that plan that uses websockets.

The problem is that after abou 20 hours I get 503 responses saying I reached my websocket limit.

The setup right now has 3 clients connecting to the service.

In the process of investigating websocket leakage in my app I would like to track the number of websockets in use.

Is there anywhere, from my app or in Azure portal, where I can see the number of active websocket connections?

Follow up:

I've logged the websocket connections as Amor suggested. The HTTP part of my app is still working, I can get dynamic results from the app which now reports what websocket connections are active and how many has been created since start.

After restarting the app service and configured one client to reconnect indefinetely.

It worked fine until the "total websocket connections" reached 350. At this time I shut down the client.

The limit should be 350 concurrent connections but it looks like it is 350 in total since start. Most (at least 340) of these connections were initiated by a single client which disposed each connection before starting a new one, it has been shutdown once the limit was reached.

I've been suggested to upgrade from Basic to Standard since standard doesn't have the artificial limitation. The only reason I can see this work would be if there is a bug in the websocket limitation for the Basic plan.

Update 2

In parallel I've been in contact with Microsoft Developer Support and they noticed what appears to be that the sockets are stuck in IIS whereas not in Kestrel. The cause of this is still being investigated.

Support could show me graphs of the connection usage over time which clearly showed how the limit was reached.

I'll keep this question updated in case there was some error in my code.

1 Answer 1

1

I suggest you define a variable to count the connections. If a web socket connection is opened, just increase the number of connections. If a web socket connection is closed, decrease the number of connections. Code below is for your reference.

Count the connections for ASP.NET SignalR.

public class MyHub : Hub
{
    private int _connectionCount = 0;

    public override Task OnConnected()
    {
        _connectionCount++;
        return base.OnConnected();
    }

    public override Task OnReconnected()
    {
        _connectionCount++;
        return base.OnReconnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        _connectionCount--;
        return base.OnDisconnected(stopCalled);
    }
}

Count the connections in traditional ASP.NET.

public class WSChatController : ApiController
{
    private int _connectionCount = 0;

    public HttpResponseMessage Get()
    {
        if (HttpContext.Current.IsWebSocketRequest)
        {
            HttpContext.Current.AcceptWebSocketRequest(ProcessWSChat);
        }
        return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
    }

    private async Task ProcessWSChat(AspNetWebSocketContext context)
    {
        WebSocket socket = context.WebSocket;
        while (true)
        {
            ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
            WebSocketReceiveResult result = await socket.ReceiveAsync(
                buffer, CancellationToken.None);
            if (socket.State == WebSocketState.Open)
            {
                _connectionCount++;
                //Process the request
            }
            else
            {
                _connectionCount--;
                break;
            }
        }
    }
}
3
  • I added a second counter which only increase for each new websocket. The result looks like a limit of 350 total(not concurrent) websocket connections.
    – hultqvist
    Jul 4, 2017 at 8:42
  • Please check whether you have closed the connection properly. Link below is for your reference. How to gracefully close a two-way WebSocket in .Net
    – Amor
    Jul 6, 2017 at 2:01
  • Hey, if I have multiple app services on one basic hosting plan, do they all share in the 350 connection limit?
    – Dan
    Sep 11, 2018 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.