private string MessageToString(ref Message message)
{
WebContentFormat messageFormat = this.GetMessageContentFormat(message);
MemoryStream ms = new MemoryStream();
XmlDictionaryWriter writer = null;
switch (messageFormat)
{
case WebContentFormat.Default:
case WebContentFormat.Xml:
writer = XmlDictionaryWriter.CreateTextWriter(ms);
break;
case WebContentFormat.Json:
writer = JsonReaderWriterFactory.CreateJsonWriter(ms);
break;
case WebContentFormat.Raw:
return this.ReadRawBody(ref message);
} message.WriteMessage(writer);
writer.Flush();
string messageBody = Encoding.UTF8.GetString(ms.ToArray()); ms.Position = ;
// 这里需要将message重新写入
XmlDictionaryReader reader;
if (messageFormat == WebContentFormat.Json)
{
reader = JsonReaderWriterFactory.CreateJsonReader(ms, XmlDictionaryReaderQuotas.Max);
}
else
{
reader = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
} Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
newMessage.Properties.CopyProperties(message.Properties);
message = newMessage; return messageBody;
} private WebContentFormat GetMessageContentFormat(Message message)
{
WebContentFormat format = WebContentFormat.Default;
if (message.Properties.ContainsKey(WebBodyFormatMessageProperty.Name))
{
WebBodyFormatMessageProperty bodyFormat;
bodyFormat = (WebBodyFormatMessageProperty)message.Properties[WebBodyFormatMessageProperty.Name];
format = bodyFormat.Format;
} return format;
} private string ReadRawBody(ref Message message)
{
XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
bodyReader.ReadStartElement("Binary");
byte[] bodyBytes = bodyReader.ReadContentAsBase64();
string messageBody = Encoding.UTF8.GetString(bodyBytes); // Now to recreate the message
MemoryStream ms = new MemoryStream();
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms);
writer.WriteStartElement("Binary");
writer.WriteBase64(bodyBytes, , bodyBytes.Length);
writer.WriteEndElement();
writer.Flush();
ms.Position = ;
XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
newMessage.Properties.CopyProperties(message.Properties);
message = newMessage; return messageBody;
}

最新文章

  1. VS2015 调试时 编辑并继续不可用
  2. 转:Unknown module(s) in QT: multimedia
  3. ajax 几种提交方式
  4. JDBC 对数据库连接的封装
  5. 原生Ajax讲解
  6. DCM TUTORIAL – AN INTRODUCTION TO ORIENTATION KINEMATICS (REV 0.1)
  7. Aisen仿新浪微博客户端项目源码
  8. 问题:关于坛友的一个js轮播效果的实现
  9. PropertyGrid--为复杂属性提供编辑功能
  10. Oracle EBS-SQL (BOM-5):检查有BOM但物料状态为NEW的物料.sql
  11. Hbuilder jQuery 自定义代码块
  12. java启动监听错误: org.springframework.web.context.ContextLoaderListener
  13. 将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置
  14. copyOfRange的应用
  15. python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验
  16. jupyter notebook中使用mpld3进行交互
  17. Charles抓包实战详解
  18. mac设置文件权限问题
  19. [洛谷P1507]NASA的食物计划 以及 对背包问题的整理
  20. 一张图解释java跨平台

热门文章

  1. Unity Dotween build error
  2. Nginx常用Rewrite(伪静态规则)WordPress/PHPCMS/ECSHOP/ShopEX/SaBlog/Discuz/DiscuzX/PHPWind/Typecho/DEDECMS
  3. 转:Spring mvc中@RequestMapping 6个基本用法小结
  4. zoj 3888 Twelves Monkeys 二分+线段树维护次小值
  5. 倍福TwinCAT(贝福Beckhoff)基础教程 松下官方软件开启报错伺服未就绪怎么办
  6. Unity里面的自动寻路(一)
  7. iOS开发:解决UIScrollView不滚动的问题
  8. C#操作消息列队
  9. IOS开发准备 资料集锦
  10. C/C++ 内存管理总结