I have a .NET Core 2.2 MVC application that is using Azure SignalR Service for real-time push of updated data from several Azure Functions. Trace logs show everything on the client working as expected. However, when the connection is successfully made, the OnConnectedAsync method on the backend hub is only firing about 10% of the time. That is, 10% of the time OnConnectedAsync fires and everything works as expected.
I have tried removing other JS files loaded client-side to see if any of them are interfering with the SignalR functions, but so far I can find nothing.
My simplified backend hub code is:
[Authorize]
public class NotificationHub : Hub
{
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
}
}
The ConfigureServices method in a Startup class includes:
services.AddMvc();
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
}).AddAzureSignalR();
The Configure method in a Startup class includes:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseAzureSignalR(routes =>
{
routes.MapHub<NotificationHub>("/notificationhub");
});
The app is using Microsoft.AspNetCore.SignalR 1.1.0, Microsoft.Azure.SignalR 1.0.10 and aspnet/[email protected].
Data updates are being pushed via several different Azure Functions using the Azure SignalR binding (via the same libraries mentioned above as well as Microsoft.Azure.WebJobs.Extensions.SignalRService 1.0.0):
[SignalR(HubName = "notificationhub")] IAsyncCollector<SignalRMessage> signalRMessages
When the client app first launches I see the same set of client side debug message, similar to:
[2019-07-06T20:49:01.625Z] Information: Normalizing '/notificationhub' to 'https://localhost:5001/notificationhub'. Utils.ts:184:30
[2019-07-06T20:49:01.626Z] Debug: Starting HubConnection. Utils.ts:187:16
[2019-07-06T20:49:01.626Z] Debug: Starting connection with transfer format 'Text'. Utils.ts:187:16
[2019-07-06T20:49:01.627Z] Debug: Sending negotiation request: https://localhost:5001/notificationhub/negotiate. Utils.ts:187:16
[2019-07-06T20:49:01.700Z] Debug: Sending negotiation request: https://insys.service.signalr.net/client/negotiate?hub=notificationhub&asrs.op=%2Fnotificationhub. Utils.ts:187:16
[2019-07-06T20:49:01.968Z] Debug: Selecting transport 'WebSockets'. Utils.ts:187:16
[2019-07-06T20:49:01.969Z] Trace: (WebSockets transport) Connecting. Utils.ts:187:16
[2019-07-06T20:49:02.127Z] Information: WebSocket connected to wss://insys.service.signalr.net/client/?hub=notificationhub&asrs.op=%2Fnotificationhub&id=gtEYI2u4eUbcEx2iKvSHQg&access_token=... Utils.ts:184:30
[2019-07-06T20:49:02.128Z] Debug: Sending handshake request. Utils.ts:187:16
[2019-07-06T20:49:02.128Z] Trace: (WebSockets transport) sending data. String data of length 32. Content: '{"protocol":"json","version":1}?'. Utils.ts:187:16
[2019-07-06T20:49:02.128Z] Information: Using HubProtocol 'json'. Utils.ts:184:30
[2019-07-06T20:49:02.344Z] Trace: (WebSockets transport) data received. String data of length 3. Content: '{}?'. Utils.ts:187:16
[2019-07-06T20:49:02.344Z] Debug: Server handshake complete.
A similar set of server side trace is generated when the OnConnectedAsync fired and when it doesn't:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST https://localhost:5001/notificationhub/negotiate text/plain;charset=UTF-8 0
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization was successful.
The only difference I can see in the trace is that:
Microsoft.AspNetCore.SignalR.HubConnectionContext:Information: Completed connection handshake. Using HubProtocol 'json'.
is included when OnConnectedAsync is successfully fired.
I've gone through all the Azure SignalR documentation and can find nothing wrong with my code.
What mechanism could be at play that only allows the client/server connection not to complete 100% of the time, even when the connection to the Azure SignalR Service is successful?