Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am facing an issue when i try to invoke a method at SignalR Server from SignalR client.

Background:

C#
Hub.Invoke("MethodToBeCalled", MessageType).Wait();


The MessageType is defined as

C#
public class MessageType
{
  primitive type prop1;
  primitive type prop2;
  List<BaseAction> Actions {get;set;}
}


BaseAction is a base class for two derived types; RunningAction and RowingAction.

Issue:
When i pass the CLR object of MessageType to the Hub.Invoke all the type information are lost when they reach the SignalRHub so basically i can't tell apart RunningAction from RowingAction because JSON Serialization just considers them all as BaseAction(BaseType).

Could anyone suggest if there is a way to workaround this problem?

Thanks in advance:)

What I have tried:

I have tried the
C#
Hub.JsonSerializer.TypeNameHandling = TypeNameHandling.All;
at the client before the connection is started. But when i do this it always fails with Server 503.

No change when trying the same at the SignalR(OWIN) server side:
C#
var myHubJsonSetting = new JsonSerializerSettings()
        {
	TypeNameHandling = TypeNameHandling.All
	};

var serializer = JsonSerializer.Create(myHubJsonSetting);

_signalR = 
WebApp.Start(
 GetStartupOptions(),
 builder =>
 {
   builder.UseCors(CorsOptions.AllowAll);
   GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
   builder.MapSignalR(
		serviceName,
		new HubConfiguration
		{
		  EnableJSONP = false,
		  EnableDetailedErrors = _detailedErrors,
		  EnableJavaScriptProxies = true
		});
   builder.RunSignalR();					
  });
Posted
Updated 6-Mar-17 19:21pm
v2

1 solution

Action is a pointer to a method. This is why it won't work. You need to tell the serializer to ignore it.

If you are using Newtonsoft's Json lib, then tag the method with the JsonIgnore attribute.

If you are using Microsoft's Json serializer, then tag the method with the Microsoft Ignore attribute.
 
Share this answer
 
v2
Comments
MohamedHassanAli 7-Mar-17 1:20am    
Oops. Sorry my bad. It is not the Action as in C# Action. I have updated the question. Thanks :)
Graeme_Grant 7-Mar-17 1:37am    
What you are asking for is possible with Newtonsoft's lib using custom JsonConverter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900