IFrank
IFrank

Reputation: 513

Call SignalR 2 method by name instead of dynamic call with C#

I'm using legacy SignalR 2 on ASP.NET (.NET Framework 4.6.2). I have my server-side class in which I call a method on the Hub "ReceiveChanges":

LegacyHubContext.Clients.All.ReceiveChanges(name, args); 

For scalability and architectural reasons, I need to call this method by name, so I'll have a string methodName which will specify the method to be called. In other words, I need to achieve the same behavior I'd have using modern SignalR in .NET Core, in which this is allowed:

HubContext.Clients.All.SendAsync(methodName, name, args);

I tried with reflection but it seems not working:

MethodInfo method = HubContextLegacy.Clients.GetType().GetMethod("All." + methodName);
method?.Invoke(HubContextLegacy.Clients, new object[] {name, args});   

method is null (not found).

Any help is much appreciated.

Upvotes: 1

Views: 23

Answers (0)

Related Questions