wcf读取message内容
2024-08-27 00:59:47
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;
}
最新文章
- VS2015 调试时 编辑并继续不可用
- 转:Unknown module(s) in QT: multimedia
- ajax 几种提交方式
- JDBC 对数据库连接的封装
- 原生Ajax讲解
- DCM TUTORIAL – AN INTRODUCTION TO ORIENTATION KINEMATICS (REV 0.1)
- Aisen仿新浪微博客户端项目源码
- 问题:关于坛友的一个js轮播效果的实现
- PropertyGrid--为复杂属性提供编辑功能
- Oracle EBS-SQL (BOM-5):检查有BOM但物料状态为NEW的物料.sql
- Hbuilder jQuery 自定义代码块
- java启动监听错误: org.springframework.web.context.ContextLoaderListener
- 将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置
- copyOfRange的应用
- python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验
- jupyter notebook中使用mpld3进行交互
- Charles抓包实战详解
- mac设置文件权限问题
- [洛谷P1507]NASA的食物计划 以及 对背包问题的整理
- 一张图解释java跨平台
热门文章
- Unity Dotween build error
- Nginx常用Rewrite(伪静态规则)WordPress/PHPCMS/ECSHOP/ShopEX/SaBlog/Discuz/DiscuzX/PHPWind/Typecho/DEDECMS
- 转:Spring mvc中@RequestMapping 6个基本用法小结
- zoj 3888 Twelves Monkeys 二分+线段树维护次小值
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下官方软件开启报错伺服未就绪怎么办
- Unity里面的自动寻路(一)
- iOS开发:解决UIScrollView不滚动的问题
- C#操作消息列队
- IOS开发准备 资料集锦
- C/C++ 内存管理总结