Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add extra parameter in SignalR method without breaking changes

Tags:

I want to pass additional parameter via Signalr without any breaking changes.

SignalR hub web api side signalr nuget package being used is "Microsoft.AspNetCore.SignalR" Version="1.0.3". Client side nuget package being used is "Microsoft.AspNetCore.SignalR.Client" Version="1.0.3" />

Initial code at signalr hub web api side is given below-

public async Task<bool> SendResponse(string uniqueId, string param1, string param2, string param3, string param4, string param5, string param6)
{
    await HubContext.Clients.Group(uniqueId).SendAsync("SendResponse", param1, param2, param3, param4, param5,param6);
    return true;
}

Initial code at client side-

Connection.On<string, string, string, string, string, string>("SendResponse",
async (param1, param2, param3, param4, param5, param6) =>
{
    //code here.
});

This client side code is shared with everyone via a nuget package and its on application owner when he want to update the nuget package.

Till now everything is working fine a changed requirement (pass a new parameter with response) arise.

To fulfill this requirement I have made below given code changes in signalr hub side-

public async Task<bool> SendResponse(string uniqueId, string param1, string param2, string param3, string param4, string param5, string param6, string param7)
{
    //send 6 parameters in old method so that client code can continue his work without updating client side code changes.
    await HubContext.Clients.Group(uniqueId).SendAsync("SendResponse", param1, param2, param3, param4, param5, param6);
    //Send 7 parameters in new method.
    await HubContext.Clients.Group(uniqueId).SendAsync("SendResponse2", param1, param2, param3, param4, param5, param6, param7);    
    return true;
}

SignalR is hub web api service updated with these code changes and issue start arise after that. Client applications who has old SignalR client code start receiving errors-

Exception thrown: 'System.IO.InvalidDataException' in Microsoft.AspNetCore.SignalR.Protocols.Json.dll
Invocation provides 7 argument(s) but target expects 0.

Changes at client side for SendResponse2 method-

Connection.On<string, string, string, string, string, string, string>("SendResponse2",
    async (param1, param2, param3, param4, param5, param6, param7) =>
    {
        //code here.
    });

Please let me know how can I fix this issue. The main important thing for me is that my end user should work fine if they have updated the client side code or not.

like image 386
Narendra Jangir Avatar asked Oct 18 '18 10:10

Narendra Jangir


People also ask

Why does SignalR disconnect?

Client disconnection scenarios When the user closes a browser window or tab, or navigates to a new page or refreshes the page, the SignalR connection immediately ends because SignalR client code handles that browser event for you and calls the Stop method.

How many connections can SignalR handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

How do I update my SignalR?

Update your Package Manager to the latest version. Install the Web Platform Installer. In the Web Platform Installer, search for and install ASP.NET and Web Tools 2013.1 for Visual Studio 2012. This will install Visual Studio templates for SignalR classes such as Hub.

Is SignalR scalable?

A SignalR app can scale out based on the number of messages sent, while the Azure SignalR Service scales to handle any number of connections.


1 Answers

Unfortunately you can't safely add a parameter right now. SignalR doesn't support the full set of C# invocation semantics. For example, we don't support overloads, or default parameters. If this is something you'd be interested in, please feel free to file an issue on https://github.com/aspnet/SignalR/issues !

A good way to make methods like this extensible is to take a single parameter object instead of multiple parameters:

public class MyParams
{
     public string Param1 { get; set; }
     public int Param2 { get; set; }
     public double Param3 { get; set; }
}

public void MyHubMethod(MyParams p)
{
}

Then, when you need to add a parameter you can add a property to this class (Param4). If an older client comes along and invokes the method without providing Param4, then that value will simply be left as the default (i.e. null for a reference type, 0 for an int, etc.).

like image 132
Andrew Stanton-Nurse Avatar answered Oct 11 '22 18:10

Andrew Stanton-Nurse