使用 BodyType - String 由旧库 (Microsoft.ServiceBus.Messaging) 读取的新库 (Microsoft.Azure.ServiceBus) 发送消息

Ily*_*dik 3 .net servicebus azure azureservicebus

我有一个前段时间写的客户端,它使用旧库并GetBody<string>()在接收消息时调用读取正文。

现在我有了新客户端Microsoft.Azure.ServiceBus(发送消息),据我所知它总是使用Stream.

所以旧客户端只是因为它期望字符串主体类型而崩溃。我发现了很多关于相反场景(新读者,旧作者)的信息,但无法弄清楚如何让新客户端以所需格式发送数据。

相关链接:

  1. 一个stackoverflow答案
  2. 互操作扩展做相反的事情(在新客户端阅读旧消息)

Sea*_*man 7

此处描述该场景。您需要按照以下方法序列化消息:

 var serializer = DataContractBinarySerializer<string>.Instance; 
 using (MemoryStream stream = new MemoryStream()) 
 {
     serializer.WriteObject(stream, some_string);
     var msg = new Message(stream.ToArray());
     var client = new Microsoft.Azure.ServiceBus.QueueClient(ConnectionString, Queue);
     await client.SendAsync(msg);
     await client.CloseAsync(); 
 }
Run Code Online (Sandbox Code Playgroud)