WCF技术内幕 第5章 (9)

5.8 Message状态

Message通过State属性管理Message对象上方法调用的顺序。

using System;

namespace System.ServiceModel.Channels
{
    // Summary:
    //     Specifies the status of a message.
    public enum MessageState
    {
        // Summary:
        //     The message has been created.
        Created = 0,
        //
        // Summary:
        //     The message is being read.
        Read = 1,
        //
        // Summary:
        //     The message has been written.
        Written = 2,
        //
        // Summary:
        //     The message has been copied.
        Copied = 3,
        //
        // Summary:
        //     The message has been closed and can no longer be accessed.
        Closed = 4,
    }
}

5.9 使用消息头

Message类型是一个SOAP消息的CLR抽象。MessageHeader是对SOAP消息头块的泛型CLR抽象,MessageHeaders是一组MessageHeader对象;EndpointAddress是对WS-Addressing endpoint规范的CLR抽象。

MessageHeader类型

            string WSAddNS="http://www.w3.org/2005/08/addressing";
            MessageHeader messageHeader = MessageHeader.CreateHeader("MessageID", WSAddNS,Guid.NewGuid().ToString());
            Console.WriteLine(messageHeader.ToString());
            //<MessageID xmlns="http://www.w3.org/2005/08/addressing">a0a8d44e-881c-49b3-88d6-73b970e18413</MessageID>
            

MessageHeader类型定义了几个序列化和编码MessageHeader对象状态的成员。

using System;
using System.IO;
using System.ServiceModel.Channels;
using System.Text;
using System.Xml;

namespace Andersoft
{
    class Program
    {
        static void Main(string[] args)
        {
            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);

            writer.Flush();
            stream.Position = 0;

            Console.WriteLine(messageHeader.ToString());
            System.IO.File.WriteAllText(@"C:\file.txt", messageHeader.ToString());

            Console.ReadKey();

//<MessageID xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.andersoft.com/messageheader">
//  <Amount xmlns="http://schemas.datacontract.org/2004/07/Andersoft">10.92</Amount>
//  <IssueDate xmlns="http://schemas.datacontract.org/2004/07/Andersoft">2013-02-25T09:49:23.7747372Z</IssueDate>
//  <PONumber xmlns="http://schemas.datacontract.org/2004/07/Andersoft">1000</PONumber>
//</MessageID>
        }
    }

    [Serializable]
    public sealed class PurchaseOrderInfo
    {
        int PONumber;
        DateTime? IssueDate;
        Double? Amount;

        public PurchaseOrderInfo(int poNumber, DateTime? issueDate, Double? amount)
        {
            PONumber = poNumber;
            IssueDate = issueDate;
            Amount = amount;
        }
    }
}

转载自:https://blog.csdn.net/xufei96/article/details/8609934

You may also like...