将DeflateStream文件内容发送到Azure消息队列

时间:2019-04-22 19:08:54

标签: c# azure deflatestream

这是关于使用C#中的TopicClient向Azure Service Message Queue收发XML文件的内容。

我正在以字符串形式发送XML文件的内容,我可以在Azure队列上看到该消息,甚至可以正确读取其内容,因此当我发送纯文本XML文件时,一切工作正常。

但是,由于队列上传入消息的数据限制,我不得不先压缩文件,然后再使用C#的DeflateStream压缩文件的内容并将其写回到文件。在接收端,我能够读取文件的内容,但与发送的文件不同。

我怀疑这与编码有关。你能指导我我所缺少的吗?预先感谢。

发件人

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

接收器

var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "<subscription name>");  
subClient.OnMessage(m =>  
{  
    Console.WriteLine(m.GetBody<string>());  
});

0 个答案:

没有答案