Consider such code on the server side -- it is dirty, but works fine:
public interface IBroadcastHub
{
Task CurrentTimeAsync(DateTimeOffset time);
}
public sealed class StrongBroadcastHub : Hub<IBroadcastHub>
{
}
public sealed class StrongHubWrapper : IBroadcastHub
{
private readonly IHubContext<StrongBroadcastHub, IBroadcastHub> _context;
public StrongHubWrapper(IHubContext<StrongBroadcastHub, IBroadcastHub> context)
{
this._context = context;
}
public Task CurrentTimeAsync(DateTimeOffset time)
{
Console.WriteLine($"Sending time to");
return _context.Clients.All.CurrentTimeAsync(time);
}
}
I can create instance of the wrapper and call CurrentTimeAsync, all fine.
The problem -- just my personal preference -- is that I would like to call CurrentTimeAsync in C# code, but expose it to the world as "Hello" or whatever.
I've read Microsoft documentation but I cannot manage to achieve this -- whenever I use strongly typed hub the method SendAsync is hidden, and when I use general hub it does not work anyway (clients seems don't receive anything). Like here:
public sealed class GenericBroadcastHub : Hub
{
}
public sealed class DynamicHubWrapper : IBroadcastHub
{
private readonly IHubContext<GenericBroadcastHub> _context;
public DynamicHubWrapper(IHubContext<GenericBroadcastHub> context)
{
this._context = context;
}
public Task CurrentTimeAsync(DateTimeOffset time)
{
Console.WriteLine($"Sending time to");
return _context.Clients.All.SendAsync(Methods.ReceiveMessage, time);
}
}
So how can I achieve this effect -- have method A in C# code, and expose it under the name B? Something like this works for ASP.Net controllers, controllers methods without a problem.
Gosh, bug in my code. Ok, I will swallow my pride :-) and leave it for educational purposes. When you are switching back and forth hubs it is essential that you don't forget to switch routing as well.
All the time I kept in the server:
"Of course" this should be changed in sync with the hub used, so keeping my example, to:
I only hope this is "the right way" (I mean, overall departure from strongly typed hub, to using dynamic names). As I wrote, MS explains HubMethodName attribute but I cannot figure out how to do it.