Message.Close メソッド

定義

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 にはファイナライザーがあり、これによって、メッセージがガベージ コレクトされると Close が自動的に呼び出されます。The 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.

メッセージを閉じた後でメソッドを呼び出したり、プロパティにアクセスしたりすると、ObjectDisposedException がスローされます。An 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.

適用対象