星空网 > 软件开发 > ASP.net

wcf读取message内容

 

 1 private string MessageToString(ref Message message)  2 {  3   WebContentFormat messageFormat = this.GetMessageContentFormat(message);  4   MemoryStream ms = new MemoryStream();  5   null;  6   switch (messageFormat)  7   {  8     case WebContentFormat.Default:  9     case WebContentFormat.10       writer = 11       break; 12     case WebContentFormat.Json: 13       writer = JsonReaderWriterFactory.CreateJsonWriter(ms); 14       break; 15     case WebContentFormat.Raw: 16       return this.ReadRawBody(ref message); 17   } 18  19   message.WriteMessage(writer); 20   writer.Flush(); 21   string messageBody = Encoding.UTF8.GetString(ms.ToArray()); 22  23   ms.Position = 0; 24   // 这里需要将message重新写入25   26   if (messageFormat == WebContentFormat.Json) 27   { 28     reader = JsonReaderWriterFactory.CreateJsonReader(ms, 29   } 30   else 31   { 32     reader = 33   } 34  35   Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version); 36   newMessage.Properties.CopyProperties(message.Properties); 37   message = newMessage; 38  39   return messageBody; 40 }41 42  private WebContentFormat GetMessageContentFormat(Message message) 43 { 44   WebContentFormat format = WebContentFormat.Default; 45   if (message.Properties.ContainsKey(WebBodyFormatMessageProperty.Name)) 46   { 47     WebBodyFormatMessageProperty bodyFormat; 48     bodyFormat = (WebBodyFormatMessageProperty)message.Properties[WebBodyFormatMessageProperty.Name]; 49     format = bodyFormat.Format; 50   } 51  52   return format; 53 }54 55  private string ReadRawBody(ref Message message) 56 { 57    message.GetReaderAtBodyContents(); 58   bodyReader.ReadStartElement("Binary"); 59   byte[] bodyBytes = bodyReader.ReadContentAsBase64(); 60   string messageBody = Encoding.UTF8.GetString(bodyBytes); 61  62   // Now to recreate the message 63   MemoryStream ms = new MemoryStream(); 64    65   writer.WriteStartElement("Binary"); 66   writer.WriteBase64(bodyBytes, 0, bodyBytes.Length); 67   writer.WriteEndElement(); 68   writer.Flush(); 69   ms.Position = 0; 70    71   Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version); 72   newMessage.Properties.CopyProperties(message.Properties); 73   message = newMessage; 74  75   return messageBody; 76 }

 




原标题:wcf读取message内容

关键词:wcf

wcf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

东南亚各国网红营销指南来了!外贸商城卖家请查收!:https://www.kjdsnews.com/a/1556781.html
做跨境电商必懂的五大流量运营逻辑,带你玩转流量市场!:https://www.kjdsnews.com/a/1556782.html
企业办理的境外直接投资ODI是什么?:https://www.kjdsnews.com/a/1556783.html
29项功能更新!独立站「爆单加速器」盘点:https://www.kjdsnews.com/a/1556784.html
2023.07.31社媒平台资讯|TikTok三大动态解读!:https://www.kjdsnews.com/a/1556785.html
SHEIN和Temu,可以达成共存吗?:https://www.kjdsnews.com/a/1556786.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流