1、消息版本函數
爲了區別消息的結構和尋址機制,W3C定製了SOAP1.1和SOAP1.2定義消息的結構,WS-Addressing 2004和WS-Addressing 1.0定義消息的尋址機制。spa
它們對應的命名空間以下:3d
SOAP1.1:http://schemas.xmlsoap.org/soap/envelope/orm
SOAP1.2:http://www.w3.org/2003/05/soap-envelopexml
WS-Addressing 2004:http://schemas.xmlsoap.org/ws/2004/08/addressing對象
WS-Addressing 1.0:http://www.w3.org/2005/08/addressingblog
Soap12WSAddressingAugust2004版本的消息:get
<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版本消息:string
<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)建立對於的消息版本,以及已經定義好的Soap11WSAddressing十、Soap11WSAddressingAugust200四、Soap十一、Soap12等消息版本。it
2、建立消息
先定義以下一個將消息寫入指定文本的方法。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>
3、消息的狀態
指定消息的狀態,消息只能被操做一次,從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能夠無限次的複製消息。