3

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)
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
  • 1
    The interop extension you're referring to is only intended for the messages coming from the old library into the new one, that are serialized using `XmlObjectSerializer`. In your case, you want to send the message as XmlObjectSerializer-serialized from the new library to the old to be able to consume. So either you do that, or you update your old client to look at the headers and indicate that a message is coming as a stream. – Sean Feldman May 08 '19 at 20:50

1 Answers1

7

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(); 
 }
Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • Thanks a lot for help, we'll try this approach! – Ilya Chernomordik May 09 '19 at 08:27
  • DataContractBinarySerializer worked for me thank you – k-dev Dec 01 '20 at 15:05
  • I don't think this works if you are trying to send to a specific subscription within a topic. The subscription is going to have it's "SQL Filter" set in order to receive specific messages. I don't think this method serializes the BrokeredMessage properties that the SQL filter is based on...does it? – WirelessG Nov 18 '21 at 18:53
  • You cannot send/publish to a specific subscription. Publishing is always done to a topic and subscriptions filter messages based on SQL or Correlation filter. It also doesn't matter what SDK is used. The logic on the server-side is the same logic. Message serialization and filter are not related. Filtering is done on either the headers or properties that can be correlated on, not the body/payload. – Sean Feldman Nov 18 '21 at 22:31