使用具有派生类型的类型调用signalr hub [英] Invoking signalr hub with a type that has derived types

查看:97
本文介绍了使用具有派生类型的类型调用signalr hub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



当我尝试从SignalR客户端调用SignalR服务器的方法时,我遇到了一个问题。



背景:



Hello,

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

Background:

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





MessageType被定义为





The MessageType is defined as

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





BaseAction是两个派生类型的基类; RunningAction和RowingAction。



问题:

当我将MessageType的CLR对象传递给Hub.Invoke时,所有类型信息都丢失了它们到达SignalRHub所以基本上我无法区分RowingAction的RunningAction,因为JSON序列化只是将它们全部视为BaseAction(BaseType)。



任何人都可以建议是否有解决这个问题的方法是什么?



提前致谢:)



我有什么试过:



我试过



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

Hub.JsonSerializer.TypeNameHandling = TypeNameHandling.All;

在连接开始之前在客户端。但是当我这样做时它总是在服务器503失败。



在SignalR(OWIN)服务器端尝试相同时没有变化:

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:

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();					
  });

推荐答案

Action 是一个指向方法的指针。这就是为什么它不起作用的原因。你需要告诉序列化程序忽略它。



如果你使用的是Newtonsoft的Json lib,那么用 JsonIgnore 属性。



如果您使用的是Microsoft的Json序列化程序,请使用Microsoft Ignore attribute。
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.


这篇关于使用具有派生类型的类型调用signalr hub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆