一、消息版本

为了区别消息的结构和寻址机制,W3C定制了SOAP1.1和SOAP1.2定义消息的结构,WS-Addressing 2004和WS-Addressing 1.0定义消息的寻址机制。

它们对应的命名空间如下:

SOAP1.1:http://schemas.xmlsoap.org/soap/envelope/

SOAP1.2:http://www.w3.org/2003/05/soap-envelope

WS-Addressing 2004:http://schemas.xmlsoap.org/ws/2004/08/addressing

WS-Addressing 1.0:http://www.w3.org/2005/08/addressing

Soap12WSAddressingAugust2004版本的消息:

<s:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.cnblogs.com/</a:Action>
</s:Header>
<s:Body>
<Order xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication4">
<Name>Order</Name>
<Price>85</Price>
</Order>
</s:Body>
</s:Envelope>

Soap11WSAddressing10版本消息:

<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.cnblogs.com/</a:Action>
</s:Header>
<s:Body>
<Order xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication4">
<Name>Order</Name>
<Price>85</Price>
</Order>
</s:Body>
</s:Envelope>

详情看MessageVersion类,可以通过CreateVersion(EnvelopeVersion envelopeVersion, AddressingVersion addressingVersion)创建对于的消息版本,以及已经定义好的Soap11WSAddressing10、Soap11WSAddressingAugust2004、Soap11、Soap12等消息版本。

二、创建消息

      先定义如下一个将消息写入指定文本的方法。writer.Formatting = Formatting.Indented;的作用是使文本Message缩进显示。

 public static void WriteMessage(Message msg, string path)
{
using (XmlTextWriter writer = new XmlTextWriter(path,Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
msg.WriteMessage(writer);
}
}

      (1)创建空白消息

Message的静态方法:public static Message CreateMessage(MessageVersion version, string action);

 static void Main(string[] args)
{
Message msg = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "http://www.cnblogs.com/");
WriteMessage(msg, @"D:\1.txt");
}

空消息为:

<s:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.cnblogs.com/</a:Action>
</s:Header>
<s:Body />
</s:Envelope>

(2)消息内容为可序列化的对象

    class Program
{
static void Main(string[] args)
{
Order order = new Order() { Name = "Order", Price = 88 };
Message msg = Message.CreateMessage(MessageVersion.Soap12, "http://www.cnblogs.com/", order);
WriteMessage(msg, @"D:\1.txt");
}
}
[DataContract]
public class Order
{
[DataMember]
public string Name; [DataMember]
public int Price;
}

1.txt中的消息为:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://www.cnblogs.com/</Action>
</s:Header>
<s:Body>
<Order xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication4">
<Name>Order</Name>
<Price>85</Price>
</Order>
</s:Body>
</s:Envelope>

(3)通过XmlReader将文件中的内容读取到消息中

 static void Main(string[] args)
{
Message msg = Message.CreateMessage(MessageVersion.Soap12, "http://www.cnblogs.com/", new XmlTextReader(@"D:\1.txt"));
WriteMessage(msg, @"D:\2.txt");
}

(4)通过XmlElement构造消息Body

static void Main(string[] args)
{
XNamespace ns = "msdn.microsoft.com";
XElement Body = new XElement(new XElement(ns + "Order",
new XElement(ns + "Name", "JDTmall"),
new XElement(ns + "Price", "85")
));
Message msg = Message.CreateMessage(MessageVersion.Soap12, "http://www.cnblogs.com/", Body);
WriteMessage(msg, @"D:\1.txt");
}

1.txt中的消息为:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://www.cnblogs.com/</Action>
</s:Header>
<s:Body>
<Order xmlns="msdn.microsoft.com">
<Name>JDTmall</Name>
<Price>85</Price>
</Order>
</s:Body>
</s:Envelope>

三、消息的状态

指定消息的状态,消息只能被操作一次,从Created到Read、Written、Copied、Closed。操作过一次的消息后无法还原,即不可能再转变成Created状态。

public enum MessageState
{
//消息已创建。
Created = 0,
//消息正在被读取。
Read = 1,
//消息已写入。
Written = 2,
//消息已复制。
Copied = 3,
// 消息已关闭,无法再进行访问。
Closed = 4,
}

(1)Created到Read

如果消息Body是一个可序列化的对象,可以通过public T GetBody<T>();将Body内容反序列化为对象,也可以通过public T GetBody<T>(XmlObjectSerializer serializer);指定序列化器。

 static void Main(string[] args)
{
Order order = new Order() { Name = "Order", Price = 88 };
Message msg = Message.CreateMessage(MessageVersion.Soap12, "http://www.cnblogs.com/", order);
Console.WriteLine("MessageState :{0}", msg.State);
Order o2 = msg.GetBody<Order>();
Console.WriteLine("MessageState After Read :{0}", msg.State);
Console.WriteLine("Message Body:{0} {1}", o2.Name, o2.Price);
}

(2)Created到Written

WriteMessage函数内会调用public void WriteMessage(XmlWriter writer);方法。

static void Main(string[] args)
{
Order order = new Order() { Name = "Order", Price = 88 };
Message msg = Message.CreateMessage(MessageVersion.Soap12, "http://www.cnblogs.com/", order);
Console.WriteLine("MessageState :{0}", msg.State);
WriteMessage(msg,@"D:\1.txt");
Console.WriteLine("MessageState After Write :{0}", msg.State);
}

(3)Created到Copied

public MessageBuffer CreateBufferedCopy(int maxBufferSize);

static void Main(string[] args)
{
Order order = new Order() { Name = "Order", Price = 88 };
Message msg = Message.CreateMessage(MessageVersion.Soap12, "http://www.cnblogs.com/", order);
Console.WriteLine("MessageState :{0}", msg.State);
MessageBuffer buffer = msg.CreateBufferedCopy(int.MaxValue);
Console.WriteLine("MessageState After Copy :{0}", msg.State);
Message msgClone = buffer.CreateMessage();
Console.WriteLine("MessageState After Createbybuffer :{0}", msgClone.State);
}

可以将Message变为MessageBuffer,随后Message变为Copied状态。通过MessageBuffer可以无限次的复制消息。

Message小结(一)的更多相关文章

  1. Message小结(二)

    当客户端调用一个WCF接口时,客户端将请求消息发送到服务端,服务端再返回回复消息.WCF内部实现了消息处理的所有细节,但是并不意味着一切不可更改.WCF也提供了一些方法让开发人员在消息发送前对消息进行 ...

  2. C#客户端的异步操作

    上篇博客[用Asp.net写自己的服务框架] 我讲述了如何实现自己的服务框架,但我想很多人应该用过WebService这类服务框架,相比起来,似乎还缺少什么东西, 是的,我也感觉到了.比如:我可以很容 ...

  3. Spring学习总结(二)——静态代理、JDK与CGLIB动态代理、AOP+IoC

    一.为什么需要代理模式 假设需实现一个计算的类Math.完成加.减.乘.除功能,如下所示: package com.zhangguo.Spring041.aop01; public class Mat ...

  4. JavaSE 异常抛光解析

    异常 异常指的是程序中的不正常现象,一般异常都是由第三方数据的使用造成的.java中每种异常现象都会有一个对应的异常类.java对异常的处理方式就是终止程序.异常机制其实是为了帮助我们找到程序中的问题 ...

  5. EventBus vs Otto vs Guava--自定义消息总线

    同步发表于http://avenwu.net/ioc/2015/01/29/custom_eventbus Fork on github https://github.com/avenwu/suppo ...

  6. 【RabbitMQ】2、RabbitMQ入门程序——Hello World

      首先说一下,MQ全称为Message Queue消息队列是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序 ...

  7. lombok使用总结

    前提 这篇文章主要介绍lombok的使用,至于lombok的源码和原理暂不探究,可以看上一篇文章插件化注解处理API去了解lombok的基本原理.参考资料: lombok官网 lombok官方教程-l ...

  8. python接口自动化(四十)- logger 日志 - 下(超详解)

    简介 按照上一篇的计划,这一篇给小伙伴们讲解一下:(1)多模块使用logging,(2)通过文件配置logging模块,(3)自己封装一个日志(logging)类.可能有的小伙伴在这里会有个疑问一个l ...

  9. [ Python入门教程 ] Python中日志记录模块logging使用实例

    python中的logging模块用于记录日志.用户可以根据程序实现需要自定义日志输出位置.日志级别以及日志格式. 将日志内容输出到屏幕 一个最简单的logging模块使用样例,直接打印显示日志内容到 ...

随机推荐

  1. Struts2-057/CVE-2018-11776两个版本RCE漏洞分析(含EXP)

    0x01 前言 2018年8月22日,Apache Strust2发布最新安全公告,Apache Struts2存在远程代码执行的高危漏洞(S2-057/CVE-2018-11776),该漏洞由Sem ...

  2. C# 实现将多个word文档合并成一个word文档的功能

    前段时间项目上遇到这么一个需求,需要将多个OCR识别的word文档合并成一个,于是就在网上找了找,自己修改了一下.在这里跟大家分享一下,希望有用的到的. 要做多word文档合并,首先要导入Micros ...

  3. efcore操作mysql,出现System.InvalidOperationException:“No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.”

    这个恶心的问题,只需要把EF的依赖换成 Pomelo.EntityFrameworkCore.MySql 库即可解决

  4. 基于STM32的平衡车机器人设计-硬件电路设计

    今天分享一个STM32F103为主控的自平衡车机器人的硬件电路设计.(亲测完全可用,且没有任何问题) 电路原理图: 电源部分采用12V锂电池作为输入,分三路稳压,其中7805作为5V传感器的供电以及后 ...

  5. .Net常用正则判断方法

    /// <summary> /// 判断string类型否为数字 /// </summary> /// <param name="strNumber" ...

  6. Day 22 面向对象知识.

    https://www.cnblogs.com/bigberg/p/7252349.html #类方法,静态方法, 属性方法. 类有两种作用:属性引用 和实例化.属性引用(类名.属性)class pe ...

  7. Memcached 查看列出所有key方法

    Memcached没有一个比较简单的方法可以直接象Redis那样keys *列出所有的Session key,并根据key get对应的session内容,但是还是可以查看的 memcached 查看 ...

  8. Swift里字符串(五)Native strings

    Native strings have tail-allocated storage, which begins at an offset of nativeBias from the storage ...

  9. iOS 关于布局问题的一些认识

    ///更新约束和布局 更新约束布局相关的API - (void)updateConstraintsIfNeeded  调用此方法,如果有标记为需要重新布局的约束,则立即进行重新布局,内部会调用upda ...

  10. 微信小程序 base64图片在canvas上画图

    上代码 wxml <canvas canvas-id="myCanvas" style="width:400px;height:400px;">&l ...