Connection failure when using SignalR with Azure APIM and an Application Gateway

58 Views Asked by At

For context, the Azure environment I'm currently working with has the following core components:

Azure APIM > Application Gateway > Web Application Firewall > Vnet > ASE > App Service

The app service is hosting a .NET 6 API that is making use of SignalR. The client I am using for testing is a .NET 6 console application.

Problem

SignalR console errors

When attempting to connect a client app using SignalR with the setup described above, the WebSocket connection attempt fails with the following error:

Failed to start connection. Error starting transport 'WebSockets'. System.Net.WebSockets.WebSocketException (0x80004005): The server's response was missing the required header 'Connection'.

SignalR then tries the fallback to use server-sent events, but it hangs for about 15 seconds before throwing the following exception:

The underlying connection closed while processing the handshake response. See exception for details. System.IO.IOException: The server disconnected before the handshake could be started.

Out of dozens of attempts to test this, there have been exactly two times where SignalR managed to fallback to long polling and that worked okay. But I haven’t nailed down any pattern to why it worked on those two occasions. And even if I did, I would still like to get the WebSocket method working.

Looking at the logs in APIM, there are no errors thrown by the backend or APIM itself:

APIM logs

The first two requests correspond to the attempted WebSocket connection, and even though they return successful responses, the client still interprets it as a failed response. I presume this is because of the missing header, however I have not found any settings on the firewall, gateway, or APIM that would be removing headers from the response.

The last two responses correspond to the attempted use of server-sent events, and here again, the responses are both 200 OK, but the client perceives it as a server disconnection.

If I take APIM out of the equation and send requests directly to the public URL exposed by the Application Gateway, the connection is established fine via WebSocket. I have also tested a connection that goes APIM > App Service with no gateway or other networking restrictions in place, and that also works fine with no issues.

Summary

  • I've verified the client logic is working fine.
  • I’ve verified the app service is also working fine.
  • If requests go client > APIM > App Service, there are no issues.
  • If requests go client > Application Gateway > Private Backend, there are no issues.
  • It is only when requests go client > APIM > Application Gateway that the WebSocket connection fails and the fall back method also fails before the handshake could be completed.

Any help is appreciated.

1

There are 1 best solutions below

1
Jason Pan On

Please follow the official blog to Import a WebSocket API in API Management.

And we also should know the limitations.

WebSocket APIs are not supported yet in the Consumption tier.

enter image description here