Send a message with a new library (Microsoft.Azure

2020-02-07 05:34发布

问题:

I have a client written for some time ago that uses the old library and does call GetBody<string>() to read the body when receiving messages.

Now I have the new client Microsoft.Azure.ServiceBus (sends messages) that as far as I understand always uses Stream.

So the old client just crashes as it expects string body type. I have found a lot of information on the opposite scenario (new reader, old writer), but cannot figure out how to make the new client send the data in required format.

Related links:

  1. A stackoverflow answer
  2. Interop extension to do the opposite (read an old message in the new client)

回答1:

The scenario is described here. You will need to serialize the message following this approach:

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