0

Msdn 说:消息还处置了在处置时用于构造正文的对象。

由此我推断关闭 Message 也会关闭它创建的 MessageBuffer。但在实际代码中并非如此。关闭消息将 messagebuffer.closed 保留为 false。

应该如何关闭消息缓冲区和从该缓冲区创建的消息?

4

1 回答 1

0

下面的源代码将帮助您了解如何使用从消息缓冲区创建的消息。

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace _22756512 {
    class Program {
        static void Main(string[] args) {
            Order order = new Order() { Id = 1, CustomerName = "Smith" };
            var message = Message.CreateMessage(MessageVersion.Default, "http://127.0.0.1/someaction", order);
            Console.WriteLine("message.state after creation: " + message.State.ToString());

            using (MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue)) {
                Console.WriteLine("message.state after create bufferedcopy: " + message.State.ToString());

                using (var anotherMessage = buffer.CreateMessage()) {
                    var anotherOrder = anotherMessage.GetBody<Order>();
                    Console.WriteLine("anotherOrder.Id = " + anotherOrder.Id);
                    Console.WriteLine("antherOrder.customername = " + anotherOrder.CustomerName);
                }

                using (var the3rdMessage = buffer.CreateMessage()) {
                    var the3rdOrder = the3rdMessage.GetBody<Order>();
                    Console.WriteLine("3rd order.id = " + the3rdOrder.Id);
                    Console.WriteLine("3rd order.customer name = " + the3rdOrder.CustomerName);
                }
            }

            message.Close();
            Console.WriteLine("message.state after close: " + message.State.ToString());
            Console.Read();
        }
    }

    public class Order {
        public Int32 Id { get; set; }
        public String CustomerName { get; set; }
    }
}
于 2014-03-31T11:04:43.677 回答