Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR Client with Multiple Parameters

I'm new to stackoverflow and has been a lurker for many years and has been a great help for me as a developer. Many thanks.

Ending my introduction with this very first post and question:

The Scenario:

I'm working with SignalR.

I have a SignalR server that broadcast a message to all the clients with 6 parameters.

When I implement it in a Web Client (MVC) it works fine and I can get all those 6 parameters.

I tried implementing it in Xamarin.

Here is the sample proxy snippet:

proxy.On<string, string, string , string, string, string>("test", (test1, test2, test3, test4, test5, test6) =>
            {
                MyActivity.RunOnUiThread(() =>
                {
                    //my method here
                });
            });

When I have 6 parameters I'll get this error:

'IHubProxy' does not contain a definition for 'On' and no extension method 'On' accepting a first argument of type 'IHubProxy' could be found (are you missing a using directive or an assembly reference?)

But when I changed my parameters to 4

proxy.On<string, string, string , string>("test", (test1, test2, test3, test4) =>
        {
            MyActivity.RunOnUiThread(() =>
            {
                //my method here
            });
        });

I would not get an error and I will be able to get those 4 params. But In my application I need to get all those 6 params.

Why is it that whenever I have more than 4 params I get this error?

Am I missing something?

Thanks!

like image 800
Light Avatar asked Oct 25 '16 06:10

Light


People also ask

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 send client specific messages using SignalR?

We change the BroadcastChartData() method to accept connectionId as an additional parameter. This way, we can find the client using the connectionId and send a message just to that client. Additionally, we add a new GetConnectionId() method, which returns the connectionId of the client.

Is SignalR bidirectional?

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available.


1 Answers

That's just a limitation on the SignalR .NET client proxy. It seems that the devs were a bit lazy about overriding the On method to support more Type parameters or maybe they just considered that if you've got more params you should group them in a class.

The solution is really simple. Create a class including as many properties as you need instead of using parameters. Something like:

public class AllParams
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
    public string Prop4 { get; set; }
    public string Prop5 { get; set; }
    public string PropN { get; set; }
}

proxy.On<AllParams>("test", all =>
{
    MyActivity.RunOnUiThread(() =>
    {
        // all.Prop1, all.Prop2, etc...
    });
});

This can even improve your code readability.

like image 56
xleon Avatar answered Oct 13 '22 23:10

xleon