Message.Close 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
關閉 Message 並釋放所有資源。
public:
void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()
範例
下列程式碼範例會示範如何建立訊息、將訊息傳送至服務以及接收回應訊息。 這時會顯示回應訊息內容,然後關閉訊息。
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。 這並非最佳做法,因為 .NET Framework 的記憶體回收 (GC) 機制不一定會在您用完非記憶體之系統資源的時候執行。 因此,當完成訊息的內容時,您一定要呼叫這個方法。 Close 方法是 Dispose (Message 也會實作此處置) 的同義資料表 (Synonym)。 在處置此訊息時,此訊息也會處置用於建構本文的物件。
在關閉之後,如果您呼叫任何方法或存取訊息的任何屬性,便會擲回 ObjectDisposedException。 在關閉訊息之後,呼叫與訊息有關之其他物件的任何方法或存取其任何屬性 (例如針對本文或標頭所傳回的訊息標頭集合、訊息屬性值或 XmlReader 執行個體),都會出現未定義的行為。