Search code examples
signalrsignalr-hub

SignalR Hub<T> implementation support for base Interface


Have a scenario where in would like to have a common Base Interface and then have derived interfaces , which then can have default base members and derived ones as per needs. Code will look something like below

public interface IBaseClient
{
    Task MessageBroadCast(dynamic msg);
}

public interface IXXClient : IBaseClient
{
    Task XXX(YYY msg);      
    //Any Additional Client methods should go here
}

public abstract class BaseHub<T> : Hub<T> where T : class 
{
    public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
    {
       // Do any logic here
        return base.OnDisconnected(stopCalled);
    }   
}
public class XXHub : BaseHub<IXXClient> 
{
  // Can Do a broadcast from here 
    // The below will work in a method and you see the MessageBroadCast with strongly typing
    Clients.All.XXX(msg);
    // Support for below is not present at least in the version I have or may be I'm missing something.
    Clients.All.MessageBroadCast(msg);
}

Problem or information needed : In SignalR 2.1 Core : Get an error message saying IXXClient implementation not provided for MessageBroadCast.

Is there any specific reason why Base Interface methods are not picked up during creating the Type by SignalR at runtime. To be specific in GenerateInterfaceImplementation(ModuleBuilder moduleBuilder)

Is this something restricted from a design perspective , It would be nice to have the ability to keep some common operations in the Base Interface. Please suggest.

Thanks for your time !.


Solution

  • This is a bug in SignalR's Hub<T> implementation. As you've discovered, there is no support for derived interfaces.

    There is no specific reason why base interface methods are not picked up. It was simply an oversight. I can say this with certainty, because I wrote the feature :(.

    In the meantime, to work around the issue, you will need to put all your client methods into a single interface. I know this is far from ideal.

    You can follow the bug report for this on GitHub. Currently, there is no milestone assigned to the bug. Feel free to make a comment on the issue to indicate that getting this bug fixed is important to you. This could help with triaging.