locked
Passing subclasses of objects to SignalR server? RRS feed

  • Question

  • User-293309416 posted

    I'm using SignalR with a .Net client. The server will be used to execute numerous "commands", but I would rather not implement a separate hub method for each command, e.g. LightOn, LightOff, VolumeUp, VolumeDown, etc.... Instead it would be nice if I could expose a single hub method such as:-

    public void ExecuteCommand(CommandBase command)

    but this approach doesn't work. If a client sends it a subclass of CommandBase, the server just gets a CommandBase object. Is there another solution? I don't want to have to go back to WCF!

    Tuesday, March 11, 2014 12:13 PM

All replies

  • User71929859 posted

    If a client sends it a subclass of CommandBase, the server just gets a CommandBase object. Is there another solution?

    How do you define the object? You should define and pass the object as show in the below link

    http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client#callserver

    Monday, March 17, 2014 4:26 AM
  • User-293309416 posted

    Thanks for the reply. I know how to pass objects between client and server. The problem is that SignalR has some restrictions when using objects as parameters, which I'm guessing has something to do with the serialization framework.

    For starters, it doesn't let you use an interface or abstract class as a hub method parameter (SignalR throws an exception). So instead I tried using a non-abstract base class as a method parameter (the "CommandBase" class, as mentioned above). When a client invokes the hub method and passes it an object that inherits from CommandBase (e.g. "TurnLightOnCommand"), what arrives at the hub method is a "CommandBase" object, and not a "TurnLightOnCommand" object. Like I say, I think it's the serialization process causing the problem.

    Monday, March 17, 2014 4:52 AM
  • User-2037315341 posted

    Hello. Were you able to resolve this problem? I'm in the same situation

    Tuesday, April 14, 2015 8:30 PM