简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM