Message.Close Message.Close Message.Close Message.Close Method

定义

关闭 Message 并释放任何资源。Closes the Message and releases any resources.

public:
 void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()

示例

下面的代码示例演示如何创建消息,将它发送给服务,并接收响应消息。The following code example shows how to create a message, send it to a service and receive a response message. 将显示响应消息的内容,然后关闭消息。The response message content is displayed and then the message is closed.

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

namespace ConsoleApplication1
{
    class client
    {


        static void RunClient()
        {
            //Step1: create a binding with just HTTP
            CustomBinding binding = new CustomBinding();
            binding.Elements.Add(new HttpTransportBindingElement());
            //Step2: use the binding to build the channel factory
            IChannelFactory<IRequestChannel> factory =
            binding.BuildChannelFactory<IRequestChannel>(
                             new BindingParameterCollection());
            //open the channel factory
            factory.Open();
            //Step3: use the channel factory to create a channel
            IRequestChannel channel = factory.CreateChannel(
               new EndpointAddress("http://localhost:8080/channelapp"));
            channel.Open();
            //Step4: create a message
            Message requestmessage = Message.CreateMessage(
                MessageVersion.Soap12WSAddressing10,
                "http://contoso.com/someaction",
                 "This is the body data");
            //send message
            Message replymessage = channel.Request(requestmessage);
            Console.WriteLine("Reply message received");
            Console.WriteLine("Reply action: {0}",
                                  replymessage.Headers.Action);
            string data = replymessage.GetBody<string>();
            Console.WriteLine("Reply content: {0}", data);
            //Step5: don't forget to close the message
            requestmessage.Close();
            replymessage.Close();
            //don't forget to close the channel
            channel.Close();
            //don't forget to close the factory
            factory.Close();
        }
        public static void Main()
        {
            Console.WriteLine("Press [ENTER] when service is ready");
            Console.ReadLine();
            RunClient();
            Console.WriteLine("Press [ENTER] to exit");
            Console.ReadLine();
        }
    }
}


Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Runtime.Serialization

Namespace ConsoleApplication1
	Friend Class client


		Private Shared Sub RunClient()
			'Step1: create a binding with just HTTP
			Dim binding As New CustomBinding()
			binding.Elements.Add(New HttpTransportBindingElement())
			'Step2: use the binding to build the channel factory
			Dim factory As IChannelFactory(Of IRequestChannel) = binding.BuildChannelFactory(Of IRequestChannel)(New BindingParameterCollection())
			'open the channel factory
			factory.Open()
			'Step3: use the channel factory to create a channel
			Dim channel As IRequestChannel = factory.CreateChannel(New EndpointAddress("http://localhost:8080/channelapp"))
			channel.Open()
			'Step4: create a message
			Dim requestmessage As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "http://contoso.com/someaction", "This is the body data")
			'send message
			Dim replymessage As Message = channel.Request(requestmessage)
			Console.WriteLine("Reply message received")
			Console.WriteLine("Reply action: {0}", replymessage.Headers.Action)
            Dim data = replymessage.GetBody(Of String)()
			Console.WriteLine("Reply content: {0}", data)
			'Step5: don't forget to close the message
			requestmessage.Close()
			replymessage.Close()
			'don't forget to close the channel
			channel.Close()
			'don't forget to close the factory
			factory.Close()
		End Sub
		Public Shared Sub Main()
			Console.WriteLine("Press [ENTER] when service is ready")
			Console.ReadLine()
			RunClient()
			Console.WriteLine("Press [ENTER] to exit")
			Console.ReadLine()
		End Sub
	End Class
End Namespace

注解

Message 确实具有一个终结器,当消息作为垃圾回收时,它会为您调用 CloseThe Message does have a finalizer, which causes Close to be called for you when the message is garbage-collected. 这不是最佳的做法,因为当系统资源不足而不是内存不足时,并不需要运行 .NET Framework 的垃圾回收 (GC) 机制。This is non-optimal, as the .NET Framework's Garbage Collection (GC) mechanism does not necessarily run when you run out of system resources other than memory. 因此,不再使用消息内容时都应该调用此方法。For this reason, you should always call this method when finished with the contents of the message. Close 方法是 Dispose(也可由 Message)的同义词。The Close method is a synonym for Dispose (which Message also implements). 释放消息时,消息也会释放用于构造正文的对象。The message also disposes the object that was used to construct the body when it is disposed.

关闭消息后,如果调用任何方法或访问消息的任何属性,都会引发 ObjectDisposedExceptionAn ObjectDisposedException is thrown if you call any method or access any properties of the message once it is closed. 关闭消息后,调用任何方法或访问与消息相关的其他对象(例如,消息头集合、消息属性值或为正文和标头返回的 XmlReader 实例)的任何属性都会出现不可确定的行为。Calling any method or accessing any properties of other objects related to the message once it is closed, (such as message header collection, message property values, or XmlReader instances returned for the body or for a header) has undefined behavior.

适用于