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!
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.
None
0 Points
2 Posts
Passing subclasses of objects to SignalR server?
Mar 11, 2014 12:13 PM|andyste|LINK
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:-
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!
All-Star
52711 Points
9668 Posts
MVP
Re: Passing subclasses of objects to SignalR server?
Mar 17, 2014 04:26 AM|Ruchira|LINK
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
Please 'Mark as Answer' if this post helps you
Developer Tools Download | Windows 10 Videos | My Tech BlogNone
0 Points
2 Posts
Re: Passing subclasses of objects to SignalR server?
Mar 17, 2014 04:52 AM|andyste|LINK
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.
None
0 Points
1 Post
Re: Passing subclasses of objects to SignalR server?
Apr 14, 2015 08:30 PM|leandrods|LINK
Hello. Were you able to resolve this problem? I'm in the same situation