How to check if a SignalR Active Connection is still active or not within .Net8 Isolated SignalRTrigger Azure Function App

Sathasivam, Logesh 0 Reputation points
2024-05-27T09:46:49.5266667+00:00

Earlier we were using InProcess and hence we had a refrence of InvocationContext which was working fine to check if the signalR connection is active or not to perform our own business logic. The same snippet follows below.

if (await invocationContext.GetClientManager().ConnectionExistsAsync(invocationContext.ConnectionId))
 { 
   //Code Logic
 }  

But now we are migrating to .Net 8 Isolated worker hence we can see the SignalRTrigger has changed from "InvocationContext" to "SignalRInvocationContext" model for worker nuget refrence.

But we can't see a similar refrence in SignalRInvocationContext to check if the connection is still existing or not. Can some one provide an equivalent implementation for this.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,566 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
131 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 6,581 Reputation points
    2024-05-28T14:21:25.57+00:00

    Hello Sathasivam, Logesh,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that your need similar "InvocationContext" to "SignalRInvocationContext" to check if a SignalR Active Connection is still active or not within .Net 8 Isolated SignalRTrigger Azure Function App.

    Therefore, in the .NET 8 Isolated worker model, the SignalRInvocationContext does not provide a direct method to check if a connection is still active. but, you can use the SignalRConnectionInfo object to get the connection information and then use the SignalRService to check the connection status. Try this sample code:

    public class Function1
    {
        private readonly ISignalRService _signalRService;
        public Function1(ISignalRService signalRService)
        {
            _signalRService = signalRService;
        }
        [FunctionName("Function1")]
        public async Task Run([SignalRTrigger] SignalRInvocationContext invocationContext)
        {
            var connectionId = invocationContext.ConnectionId;
            var hubName = invocationContext.HubName;
            var connectionExists = await _signalRService.ConnectionExistsAsync(hubName, connectionId);
            if (connectionExists)
            {
                // Your code logic here
            }
        }
    }
    

    In this code, ISignalRService is a service that you would need to implement. This service would use the Azure.SignalR.Management package to interact with the Azure SignalR Service and check if a connection exists.

    Remember to replace Function1 and hubName with your actual Azure Function and SignalR hub names. Also, you would need to register ISignalRService in your startup class.

    Accept Answer

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    0 comments No comments