WS-Addressing終結點規范 WS-Addressing為SOAP消息提供了一種統一的、標准化的尋址方式,這個標准的核心之一就是終結點規范。 MessageHeader類型是對SOAP消息頭塊的CLR抽象,可以假定MessageHeader對象是對Endpoint參考規范的CLR抽象。 IsReferenceParameter只讀屬性表示MessageHeader對象是否為一個WS-Addressing引用參數或引用屬性。 MessageHeaders類型 SOAP消息可能包含很多消息頭塊,表示一組消息頭塊對象的方法,MessageHeaders就是這個作用。 MessageHeaders類型的只讀屬性Headers是在Message裡增加,修改,查詢和移除MessageHeader的主要方式。 MessageHeaders是一個具體類,而不是抽象類,它不包含工廠方法。 添加MessageHeader: [csharp] MessageHeaders headers = new MessageHeaders(MessageVersion.Soap12WSAddressingAugust2004); headers.Add(messageHeader); headers.Insert(0, messageHeader); MessageHeaders headers = new MessageHeaders(MessageVersion.Soap12WSAddressingAugust2004); headers.Add(messageHeader); headers.Insert(0, messageHeader); 獲取MessageHeader的值: [csharp] urchaseOrderInfo purchaseOrderInfo = new PurchaseOrderInfo(1000, DateTime.UtcNow, 10.92d); string WSAddNS="http://www.andersoft.com/messageheader"; MessageHeader messageHeader = MessageHeader.CreateHeader("MessageID", WSAddNS, purchaseOrderInfo); Stream stream = new MemoryStream(); XmlDictionaryWriter writer=XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8); messageHeader.WriteHeader(writer, MessageVersion.Soap12WSAddressingAugust2004); MessageHeaders headers = new MessageHeaders(MessageVersion.Soap12WSAddressingAugust2004); headers.Insert(0, messageHeader); writer.Flush(); stream.Position = 0; Console.WriteLine(headers.GetHeader<PurchaseOrderInfo>(0).PONumber); //1000 Console.WriteLine(headers.FindHeader(messageHeader.Name, messageHeader.Namespace)); //0 PurchaseOrderInfo purchaseOrderInfo = new PurchaseOrderInfo(1000, DateTime.UtcNow, 10.92d); string WSAddNS="http://www.andersoft.com/messageheader"; MessageHeader messageHeader = MessageHeader.CreateHeader("MessageID", WSAddNS, purchaseOrderInfo); Stream stream = new MemoryStream(); XmlDictionaryWriter writer=XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8); messageHeader.WriteHeader(writer, MessageVersion.Soap12WSAddressingAugust2004); MessageHeaders headers = new MessageHeaders(MessageVersion.Soap12WSAddressingAugust2004); headers.Insert(0, messageHeader); writer.Flush(); stream.Position = 0; Console.WriteLine(headers.GetHeader<PurchaseOrderInfo>(0).PONumber); //1000 Console.WriteLine(headers.FindHeader(messageHeader.Name, messageHeader.Namespace)); //0 復制MessageHeaders對象 [csharp] Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "urn:SomeAction", "Hello WCF"); message.Headers.Add(MessageHeader.CreateHeader("test", "test", "test")); message.Headers.To = new Uri("http://www.andersoft.com/Anders"); Message message2 = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "urn:SomeAction2", "Hello WCF"); message2.Headers.CopyHeadersFrom(message); Console.WriteLine(message2.ToString()); //<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">urn:SomeAction2</a:Action> // <a:Action s:mustUnderstand="1">urn:SomeAction</a:Action> // <test xmlns="test">test</test> // <a:To s:mustUnderstand="1">http://www.andersoft.com/Anders</a:To> // </s:Header> // <s:Body> // <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello WCF</string> // </s:Body> //</s:Envelope> Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "urn:SomeAction", "Hello WCF"); message.Headers.Add(MessageHeader.CreateHeader("test", "test", "test")); message.Headers.To = new Uri("http://www.andersoft.com/Anders"); Message message2 = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "urn:SomeAction2", "Hello WCF"); message2.Headers.CopyHeadersFrom(message); Console.WriteLine(message2.ToString()); //<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">urn:SomeAction2</a:Action> // <a:Action s:mustUnderstand="1">urn:SomeAction</a:Action> // <test xmlns="test">test</test> // <a:To s:mustUnderstand="1">http://www.andersoft.com/Anders</a:To> // </s:Header> // <s:Body> // <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello WCF</string> // </s:Body> //</s:Envelope> 序列化MessageHeaders對象 [csharp] Stream stream = new MemoryStream(); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8); message2.Headers.WriteHeader(0, writer); Stream stream = new MemoryStream(); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8); message2.Headers.WriteHeader(0, writer); MessageHeaders類型定義了與終結點相關的屬性:From、ReplyTo,FaultTo和To,以及Action,MessageId和RelatesTo。 [csharp] message2.Headers.From = new EndpointAddress("http://www.andersoft.com/message2"); message2.Headers.ReplyTo = new EndpointAddress("http://www.andersoft.com/ReplyTo"); message2.Headers.FaultTo = new EndpointAddress("http://www.andersoft.com/FaultTo"); message2.Headers.To = new Uri("http://www.andersoft.com/To"); message2.Headers.Action = "Message2"; message2.Headers.MessageId = new UniqueId(Guid.NewGuid()); message2.Headers.RelatesTo = new UniqueId(Guid.NewGuid()); message2.Headers.From = new EndpointAddress("http://www.andersoft.com/message2"); message2.Headers.ReplyTo = new EndpointAddress("http://www.andersoft.com/ReplyTo"); message2.Headers.FaultTo = new EndpointAddress("http://www.andersoft.com/FaultTo"); message2.Headers.To = new Uri("http://www.andersoft.com/To"); message2.Headers.Action = "Message2"; message2.Headers.MessageId = new UniqueId(Guid.NewGuid()); message2.Headers.RelatesTo = new UniqueId(Guid.NewGuid()); UniqueId類:www.2cto.com [csharp] Assembly System.Runtime.Serialization.dll, v4.0.30319 namespace System.Xml { // Summary: // A unique identifier optimized for Guids. public class UniqueId Assembly System.Runtime.Serialization.dll, v4.0.30319 namespace System.Xml { // Summary: // A unique identifier optimized for Guids. public class UniqueId