BindingElement 類別

定義

繫結的項目,這些繫結可以建置各種通道型別的通道處理站和通道接聽程式,以用來處理傳出和傳入的訊息。

public ref class BindingElement abstract
public abstract class BindingElement
type BindingElement = class
Public MustInherit Class BindingElement
繼承
BindingElement
衍生

範例

下列範例顯示如何新增傳輸繫結項目至自訂繫結,並接著建置可以接收傳入訊息的通道接聽程式。

CustomBinding binding = new CustomBinding();
binding.Elements.Add(new HttpTransportBindingElement());
BindingParameterCollection paramCollection = new BindingParameterCollection();
IChannelListener<IReplyChannel> listener = binding.BuildChannelListener<IReplyChannel>
    (new Uri("http://localhost:8000/ChannelApp"), paramCollection);

listener.Open();
IReplyChannel channel = listener.AcceptChannel();
Console.WriteLine("Listening for messages");
channel.Open();
RequestContext request = channel.ReceiveRequest();
Message msg = request.RequestMessage;
Console.WriteLine("Message Received");
Console.WriteLine("Message Action: {0}", msg.Headers.Action);

if (msg.Headers.Action == "hello")
{
    Message reply = Message.CreateMessage(MessageVersion.Default, "wcf");
    request.Reply(reply);
}

msg.Close();
channel.Close();
listener.Close();
Dim binding As New CustomBinding()
binding.Elements.Add(New HttpTransportBindingElement())
Dim paramCollection As New BindingParameterCollection()

Dim listener As IChannelListener(Of IReplyChannel)
listener = binding.BuildChannelListener(Of IReplyChannel)(New Uri("http://localhost/channelApp"), paramCollection)

listener.Open()
Dim channel As IReplyChannel = listener.AcceptChannel()
Console.WriteLine("Listening for messages")
channel.Open()
Dim request As RequestContext = channel.ReceiveRequest()
Dim msg As Message = request.RequestMessage
Console.WriteLine("Message Received")
Console.WriteLine("Message Action: {0}", msg.Headers.Action)

If (msg.Headers.Action = "hello") Then

    Dim reply As Message = Message.CreateMessage(MessageVersion.Default, "wcf")
    request.Reply(reply)
End If

msg.Close()
channel.Close()
listener.Close()

備註

由繼承自這個抽象基底類別之已排序繫結項目集合所組成的繫結。 TransportBindingElement 類別繼承自 BindingElement 類別。

如果您只要使用通道模型,則可選擇性地為您的傳輸建立繫結和繫結項目。 只要通道處理站和接聽程式是公開的,您就可以透過它們來執行各種工作。

Windows Communication Foundation (WCF) 服務模型會使用處理站模式來建立通道堆疊。 如果您想要使用 WCF 服務模型,則需要使用傳輸繫結項目。 將此繫結程序項目放入繫結程序是最好的做法,因為這樣使用者就不需要為您的傳輸建立自己的自訂繫結程序。 最好同時建立繫結和繫結項目,並將通道處理站和接聽程式隱藏在組件中。

在傳送端,繫結會用來建置 IChannelFactory,而此處理站會接著建置通道堆疊,並傳回在堆疊中頂端通道的參考。 應用程式可以接著使用這個通道來傳送訊息。

同樣地,在接收端,繫結會用來建置 IChannelListener,而此接聽程式會接聽傳入訊息。 IChannelListener 會透過建立通道堆疊並將應用程式參考傳遞至頂端通道的方式,來提供訊息給接聽應用程式。 應用程式可以接著使用這個通道來接收傳入訊息。

注意

如果您要新增必須加密的訊息標頭,則必須在系統要求 ChannelProtectionRequirements 時,從 GetProperty<T>(BindingContext) 方法傳回 ChannelProtectionRequirements 具有需求的實例。

建構函式

BindingElement()

初始化 BindingElement 類別的新執行個體。

BindingElement(BindingElement)

從指定的繫結項目初始化 BindingElement 類別的新執行個體。

方法

BuildChannelFactory<TChannel>(BindingContext)

初始化通道處理站,以便從繫結內容產生指定型別的通道。

BuildChannelListener<TChannel>(BindingContext)

初始化通道接聽項,以便從繫結內容接受指定之類型的通道。

CanBuildChannelFactory<TChannel>(BindingContext)

傳回值,指出繫結項目是否可以建置 (Build) 特定通道類型的通道處理站。

CanBuildChannelListener<TChannel>(BindingContext)

傳回值,指出繫結項目是否可以建置特定通道型別的通道接聽程式。

Clone()

在衍生類別中覆寫時,傳回繫結項目物件的複本。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetProperty<T>(BindingContext)

在衍生類別中覆寫時,從繫結堆疊的適當層次中,傳回要求的型別物件 (如果有的話)。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於