0

This is about sending/receiving contents of an XML file to/from Azure Service Message Queue using TopicClient in C#.

I am sending contents of a XML file as string, I can see the message on the Azure Queue and can even read its contents properly, so everything works fine when I send a plain text XML file.

But, due to data restrictions on the incoming message on the queue, I had to compress the file before sending it, I am using the C#'s DeflateStream to compress the contents of the file and writing it back to a file. On the receiving end, I am able to read the contents of the file but its not the same as what was sent.

I suspect this is has got something to do with the encoding. Could you please guide me on what I am missing? Thanks in advance.

Sender

public string Compress(FileInfo XMLFile) {

 using(FileStream originalFileStream = file.OpenRead()) {
  if ((File.GetAttributes(file.FullName) & FileAttributes.Hidden) !=
   FileAttributes.Hidden & file.Extension != ".cmp") {
   using(FileStream compressedFileStream = File.Create(file.FullName + ".cmp")) {
    using(DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionMode.Compress)) {
     originalFileStream.CopyTo(compressionStream);
    }
   }

   FileInfo info = new FileInfo(directoryPath + "\\" + file.Name + ".cmp");
   Console.WriteLine("Compressed {0} from {1} to {2} bytes.", file.Name, file.Length, info.Length);
  }
 }

 return info.FullName;
}

// snippet from the send method
FileInfo XMLfile = new FileInfo(XMLFilePath);
string CompressedXMLFilePath = Compress(XMLfile);
TopicClient myTopicClient = TopicClient.CreateFromConnectionString(AzureConnectionString);
string toSend = File.ReadAllText(CompressedXMLFilePath); // read contents of file compressed with DeflateStream.
myTopicClient.Send(new BrokeredMessage(toSend));

Receiver

var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "<subscription name>");  
subClient.OnMessage(m =>  
{  
    Console.WriteLine(m.GetBody<string>());  
});
Timmy
  • 1
  • 2
  • `ReadAllText(CompressedXMLFilePath);` should not be able to work. I can't really reconstruct your dataflow from the question. Deflated text is no longer text. – H H Apr 22 '19 at 20:10
  • @HenkHolterman - Updated the code, hope it is clear now. I write the compressed stream to a file with extension .cmp, and then I read it back into a string and send it to the queue. – Timmy Apr 22 '19 at 21:40
  • Instead of reading the compressed file as string before sending, read that as stream (or convert it to byte array) and send that. Similarly while reading, read the data accordingly. As mentioned in other comments, once you compress the file, it is no longer a string. – Gaurav Mantri Apr 23 '19 at 12:13
  • Thanks to pointing me to the right direction. I'm using MemoryStream to send and now everything works as expected. – Timmy Apr 24 '19 at 15:46

0 Answers0