Invoking azure functions server SignalR group functions from ASP.NET Core Client

Bubba Jones 206 Reputation points
2023-02-22T11:00:39.4466667+00:00

I am trying to invoke azure functions server SignalR group functions from an ASP.NET Core client. The server side code that I am trying to invoke via an ASP.NET Core client can be found in this article. I have not found a single reference of a C# client in ASP.NET Core that actually invokes the functions in the aforementioned article. I have only landed upon javascript clients that work against other types of hub servers.

I tried creating this server side code:

    [Function("AddToGroup")]
    [SignalROutput(HubName = "serverless", ConnectionStringSetting = "SignalRConnection")]
    public static SignalRGroupAction AddToGroup([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
    {
        return new SignalRGroupAction(SignalRGroupActionType.Add)
        {
            GroupName = "group1",
            UserId = "user1"
        };
    }

And invoking it with the following to no avail:

        HubConnection _connection = new HubConnectionBuilder().WithUrl("http://localhost:7071/api").Build();
        Dispatcher.Dispatch(async () => await _connection.StartAsync());

	//	This does not work 		
        await HubConnectionExtensions.InvokeAsync(_connection, "AddToGroup");

Can someone fill me in on how to invoke server side SignalR azure functions using an ASP.NET Core client?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,310 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,199 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
120 questions
{count} votes

Accepted answer
  1. Liangying Wei 756 Reputation points Microsoft Employee
    2023-03-14T01:22:34.95+00:00

    C# client follows the SignalR client guides https://learn.microsoft.com/aspnet/core/signalr/dotnet-client?view=aspnetcore-6.0&tabs=visual-studio#connect-to-a-hub

    As similar to how we do it in the javascript client, in C#, if you have the below method defined in your function ( sample here https://github.com/aspnet/AzureSignalR-samples/tree/main/samples/BidirectionChat):

    
    
            [FunctionName(nameof(SendToGroup))]
            public async Task SendToGroup([SignalRTrigger]InvocationContext invocationContext, string groupName, string message)
            {
                await Clients.Group(groupName).SendAsync(NewMessageTarget, new NewMessage(invocationContext, message));
            }
    

    Or if you are using isolated worker process(sample here https://github.com/aspnet/AzureSignalR-samples/tree/main/samples/DotnetIsolated-BidirectionChat):

    
    
            [Function("SendToGroup")]
            [SignalROutput(HubName = "Hub")]
            public SignalRMessageAction SendToGroup([SignalRTrigger("Hub", "messages", "SendToGroup", "groupName", "message")] SignalRInvocationContext invocationContext, string groupName, string message)
            {
                return new SignalRMessageAction("newMessage")
                {
                    GroupName = groupName,
                    Arguments = new object[] { new NewMessage(invocationContext, message) }
                };
            }
    
    

    The client-side invoke this method by calling InvokeAsync:

    
    var connection = new HubConnectionBuilder().WithUrl("http://localhost:7071/api").Build();
    await connection.StartAsync();
    await connection.InvokeAsync("SendToGroup", "group1", "message1");
           
    

    Also don't forget to setup the Upstream settings in your service (as described in the sample provided) so that the service can pass the call to your azure function.

    2 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Liangying Wei 756 Reputation points Microsoft Employee
    2023-03-13T01:48:22.16+00:00

    Your current Azure Function code uses an HTTP trigger [HttpTrigger(AuthorizationLevel.Anonymous, "post")] so you can simply use HTTP POST to trigger this function.

    If you'd like your SignalR client to invoke this method through SignalR protocol, please follow https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service-trigger?tabs=in-process&pivots=programming-language-javascript to use a SignalRTrigger.

    1 person found this answer helpful.

  2. MuthuKumaranMurugaachari-MSFT 22,151 Reputation points
    2023-03-02T16:48:54.0533333+00:00

    Bubba Jones Thanks for posting your question in Microsoft Q&A. Above code snippet/article refers to SignalR output binding to send messages to all or connected clients in the Azure SignalR service. Meaning, once this azure function is invoked, it will send the message defined above to user user1 (connected to SignalR).
    Trigger (to invoke azure function) is defined as HttpTrigger which means you can invoke this azure function using HTTP call like described in doc with endpoint http://<APP_NAME>.azurewebsites.net/api/AddToGroup from your ASP.NET core application (just like any other Rest API).

    But if you are looking for ASP.NET Core client application to connect directly to Azure SignalR service (defined in SignalRConnection), then you can review QuickStart example to create the client application. More info also found here: Azure Functions development and configuration with Azure SignalR Service.

    I hope this helps with your question. If my understanding is incorrect, please elaborate more about your scenario to understand better.


  3. Ryan Hill 26,056 Reputation points Microsoft Employee
    2023-03-07T00:06:14.73+00:00

    Hi @Bubba Jones

    Have a look at this Blazor Server chat app. This C# example should get you closer to implementing a SiganlR client in C#. Remember, Blazor is the only client runtime you'll find that uses C# because it leverages wasm, so when looking for examples, look under Blazor.