简体   繁体   English

Azure服务总线序列化问题[C#/ JAVA]

[英]Azure Service Bus Serialization Issues [C# / JAVA]

I have some problems with serialization in c# and java cross platform: 我在C#和Java跨平台中进行序列化时遇到一些问题:

We have 2 apps using ServiceBus, a C# one and a JAVA one. 我们有2个使用ServiceBus的应用程序,一个是C#,另一个是JAVA。 Lets say C# is in charge of publishing data to a queue / topic and JAVA is subscribing to it. 可以说C#负责将数据发布到队列/主题,而JAVA正在订阅它。

In C# we use the standard publish method which allows generic types publish so it automatically serializes data sent with TypeA class. 在C#中,我们使用标准的publish方法,该方法允许泛型类型发布,因此它会自动序列化使用TypeA类发送的数据。

Then in java we have TypeA equivalent coded, but cannot find any API-Deserializer that can deserialize the object to our . 然后在Java中,我们已经编码了TypeA等效项,但是找不到任何可以将对象反序列化为的API-Deserializer。

Is there any valid deserializer for JAVA or we must serialize (for example using JSON) in C#, publish it and deserialize in JAVA with any JSON deserializer? JAVA是否有任何有效的反序列化器,或者我们必须在C#中序列化(例如使用JSON),然后使用任何JSON反序列化器将其发布并在JAVA中反序列化?

Thanks in advance! 提前致谢!

I believe one of the DataContractSerializers will be used if you don't do anything on the BrokeredMessage. 我相信,如果您对BrokeredMessage不做任何操作,将使用其中一个DataContractSerializers。 You didn't show any code, but I assume you are using BrokeredMessage class. 您没有显示任何代码,但是我假设您正在使用BrokeredMessage类。 If you want cross-platform, you need to control the serialization. 如果要跨平台,则需要控制序列化。 I would serialize to JSON and either pass the JSON as a string, or use the stream overload and perhaps compress the stream, passing that in the constructor (if it was a big object). 我将序列化为JSON并以字符串形式传递JSON,或者使用流过载并压缩流,然后将其传递到构造函数中(如果它是一个大对象)。 The Java client would need to get a JSON deserializer and do the reverse. Java客户端需要获取JSON反序列化器并进行相反的操作。

As you have figured out - the DataContractSerializer is not easily reversible in Java. 如您所知-DataContractSerializer在Java中不容易逆转。 I am sure you could do it, but using JSON or protobuf is a way easier and cross platform way. 我相信您可以做到,但是使用JSON或protobuf是一种更容易的跨平台方式。

For cross-language binary serialization, refer to this question . 对于跨语言二进制序列化, 请参阅此问题 However, I would use JSON in this case - I believe it would be much simpler. 但是,在这种情况下,我将使用JSON-我相信它将简单得多。

I guess the answer is pertaining to your existing codebase. 我想答案与您现有的代码库有关。 The most orthodox way would be use XmlSerializer on C#, and JAXB on Java. 最正统的方法是在C#上使用XmlSerializer,在Java上使用JAXB。 Of course, you need manual examination of code in both language to ensure full interoperability. 当然,您需要手动检查两种语言的代码,以确保完全的互操作性。

I think both languages support multiple ways of serialization/deserialization, you just need to find the one that are compatible to each other. 我认为两种语言都支持多种序列化/反序列化方式,您只需要找到一种相互兼容的方式即可。

  1. Use your choice of serializing library in C# rather than default contract serializer. 在C#中使用您选择的序列化库,而不要使用默认合同序列化器。 (I'll be using JSON and Newtonsoft.json) (我将使用JSON和Newtonsoft.json)

  2. Publish message as json text stream 将消息发布为JSON文本流

      var json = Newtonsoft.Json.Linq.JObject.FromObject(yourSerializableObject).ToString(); var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(json); writer.Flush(); stream.Position = 0; _topicClient.Send(new BrokeredMessage(stream, true) { ContentType = "application/json" }); 
  3. Receive message as json text stream 接收消息作为json文本流

      var message = _subscriptionClient.Receive(); Stream stream = message.GetBody<Stream>(); StreamReader reader = new StreamReader(stream); string s = reader.ReadToEnd(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM