WCF技术内幕 第5章 (8)

5.5 Buffered vs Streamed 消息

假设当程序接收到一个Message时,它已经知道了整个Message,这种方式成为缓存模式(buffering)。与之相对的就是流处理模式。

在WCF里,Message的消息头块通常是缓存起来的,消息内容可以采用buffered或streamed模式。缓存区的消息模式为64kb,如果消息体是streamed模式,它的大小就是无限制的。小的消息就不需要采用streamed模式,采用缓存模式更加高效。


5.6 序列化消息

消息的实际序列化工作由XmlWriter或XmlDictionaryWriter对象完成,而不是直接由Message对象完成。

        //
        // Summary:
        //     Writes the body element using the specified System.Xml.XmlDictionaryWriter.
        //
        // Parameters:
        //   writer:
        //     An System.Xml.XmlDictionaryWriter object to be used to write the body element.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        public void WriteBody(XmlDictionaryWriter writer);
        //
        // Summary:
        //     Serializes the message body using the specified System.Xml.XmlWriter.
        //
        // Parameters:
        //   writer:
        //     The System.Xml.XmlWriter object to be used to write the body of the message.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        //
        //   System.ObjectDisposedException:
        //     The message is disposed.
        public void WriteBody(XmlWriter writer);
        //
        // Summary:
        //     Serializes the body content using the specified System.Xml.XmlDictionaryWriter.
        //
        // Parameters:
        //   writer:
        //     An System.Xml.XmlDictionaryWriter object to be used to write the body element.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        //
        //   System.ObjectDisposedException:
        //     The message is closed.
        //
        //   System.InvalidOperationException:
        //     The message has been copied, read or written.
        public void WriteBodyContents(XmlDictionaryWriter writer);
        //
        // Summary:
        //     Serializes the entire message using the specified System.Xml.XmlDictionaryWriter.
        //
        // Parameters:
        //   writer:
        //     An System.Xml.XmlDictionaryWriter object to be used to write the message.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        //
        //   System.ObjectDisposedException:
        //     The message is closed.
        //
        //   System.InvalidOperationException:
        //     The message has been copied, read or written.
        public void WriteMessage(XmlDictionaryWriter writer);
        //
        // Summary:
        //     Serializes the entire message using the specified System.Xml.XmlWriter.
        //
        // Parameters:
        //   writer:
        //     The System.Xml.XmlWriter object to be used to write the body of the message.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        //
        //   System.ObjectDisposedException:
        //     The message is closed.
        //
        //   System.InvalidOperationException:
        //     The message has been copied, read or written.
        public void WriteMessage(XmlWriter writer);
        //
        // Summary:
        //     Serializes the start body using the specified System.Xml.XmlDictionaryWriter.
        //
        // Parameters:
        //   writer:
        //     An System.Xml.XmlDictionaryWriter object to be used to write the start body.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        public void WriteStartBody(XmlDictionaryWriter writer);
        //
        // Summary:
        //     Serializes the start body using the specified System.Xml.XmlDictionaryWriter.
        //
        // Parameters:
        //   writer:
        //     The System.Xml.XmlDictionaryWriter object to be used to write the start body
        //     of the message.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        public void WriteStartBody(XmlWriter writer);
        //
        // Summary:
        //     Serializes the start envelope using the specified System.Xml.XmlDictionaryWriter.
        //
        // Parameters:
        //   writer:
        //     An System.Xml.XmlDictionaryWriter object to be used to write the start envelope.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     writer is null.
        public void WriteStartEnvelope(XmlDictionaryWriter writer);

WriteMessage方法序列化消息的全部内容到XmlWriter或XmlDictionaryWriter包装的Stream里。


5.7 反序列化消息

消息的反序列化是从一个序列化的消息中创建一个新的消息的别称。

        //
        // Summary:
        //     Retrieves the body of this System.ServiceModel.Channels.Message instance.
        //
        // Type parameters:
        //   T:
        //     The body of the message.
        //
        // Returns:
        //     An object of type T that contains the body of this message.
        public T GetBody<T>();
        //
        // Summary:
        //     Retrieves the body of this System.ServiceModel.Channels.Message using the
        //     specified serializer.
        //
        // Parameters:
        //   serializer:
        //     A System.Runtime.Serialization.XmlObjectSerializer.
        //
        // Type parameters:
        //   T:
        //     The body of the message.
        //
        // Returns:
        //     An object of type T that contains the body of this message.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     serializer is null.
        public T GetBody<T>(XmlObjectSerializer serializer);

检查Message是否是SOAP Fault
IsFault属性会标记Message是否是一条SOAP错误消息。
        private static void CreateAndShowMessage(MessageFault messageFault, MessageVersion version)
        {
            Message message = Message.CreateMessage(version, messageFault, "urn:SomeFaultAction");
            Console.WriteLine("message is {0}a SOAP fault", message.IsFault ? string.Empty : "not ");
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值