BindingElement 類別

定義

繫結的項目,這些繫結可以建置各種通道型別的通道處理站和通道接聽程式,以用來處理傳出和傳入的訊息。The elements of the bindings that build the channel factories and channel listeners for various types of channels that are used to process outgoing and incoming messages.

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

範例

下列範例顯示如何新增傳輸繫結項目至自訂繫結,並接著建置可以接收傳入訊息的通道接聽程式。The following example shows how to add a transport binding element to a custom binding and then build a channel listener that can accept incoming messages.

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()

備註

由繼承自這個抽象基底類別之已排序繫結項目集合所組成的繫結。A binding consists of an ordered set of binding elements that inherit from this abstract base class. TransportBindingElement 類別繼承自 BindingElement 類別。The TransportBindingElement class inherits from the BindingElement class.

如果您只要使用通道模型,則可選擇性地為您的傳輸建立繫結和繫結項目。Creating a binding and binding element for your transport is optional if you are just using the channel model. 只要通道處理站和接聽程式是公開的,您就可以透過它們來執行各種工作。It is possible to do everything you need through the channel factory and listener as long as they are made public.

Windows Communication Foundation (WCF)服務模型會使用 factory 模式,其中系結會用來建立通道堆疊。The Windows Communication Foundation (WCF) service model uses a factory pattern where the binding is used to create the channel stack. 如果您想要使用 WCF 服務模型,則需要使用傳輸繫結項目。If you want to use the WCF service model, then using a transport binding element is required. 將此繫結程序項目放入繫結程序是最好的做法,因為這樣使用者就不需要為您的傳輸建立自己的自訂繫結程序。Placing this binding element into a binding is a good practice because it removes the requirement for users to create their own custom binding for your transport. 最好同時建立繫結和繫結項目,並將通道處理站和接聽程式隱藏在組件中。It is best to create both a binding and binding element, and hide the channel factory and listener inside the assembly.

在傳送端,繫結會用來建置 IChannelFactory,而此處理站會接著建置通道堆疊,並傳回在堆疊中頂端通道的參考。On the sending side, a binding is used to build a IChannelFactory, which in turn builds a channel stack and returns a reference to the top channel in the stack. 應用程式可以接著使用這個通道來傳送訊息。The application can then use this channel to send messages.

同樣地,在接收端,繫結會用來建置 IChannelListener,而此接聽程式會接聽傳入訊息。Similarly, on the receiving side a binding is used to build a IChannelListener, which listens for incoming messages. IChannelListener 會透過建立通道堆疊並將應用程式參考傳遞至頂端通道的方式,來提供訊息給接聽應用程式。The IChannelListener provides messages to the listening application by creating channel stacks and handing the application a reference to the top channel. 應用程式可以接著使用這個通道來接收傳入訊息。The application then uses this channel to receive incoming messages.

注意

如果您要加入必須加密的訊息標頭,您必須在要求 ChannelProtectionRequirements 時,從 GetProperty<T>(BindingContext) 方法傳回 @no__t 0 實例。If you are adding message headers that must be encrypted you must return a ChannelProtectionRequirements instance with your requirements from the GetProperty<T>(BindingContext) method when asked for ChannelProtectionRequirements.

建構函式

BindingElement()

初始化 BindingElement 類別的新執行個體。Initializes a new instance of the BindingElement class.

BindingElement(BindingElement)

從指定的繫結項目初始化 BindingElement 類別的新執行個體。Initializes a new instance of the BindingElement class from a specified binding element.

方法

BuildChannelFactory<TChannel>(BindingContext)

初始化通道處理站,以便從繫結內容產生指定型別的通道。Initializes a channel factory for producing channels of a specified type from the binding context.

BuildChannelListener<TChannel>(BindingContext)

初始化通道接聽項,以便從繫結內容接受指定之類型的通道。Initializes a channel listener to accept channels of a specified type from the binding context.

CanBuildChannelFactory<TChannel>(BindingContext)

傳回值,指出繫結項目是否可以建置 (Build) 特定通道類型的通道處理站。Returns a value that indicates whether the binding element can build a channel factory for a specific type of channel.

CanBuildChannelListener<TChannel>(BindingContext)

傳回值,指出繫結項目是否可以建置特定通道型別的通道接聽程式。Returns a value that indicates whether the binding element can build a listener for a specific type of channel.

Clone()

在衍生類別中覆寫時,傳回繫結項目物件的複本。When overridden in a derived class, returns a copy of the binding element object.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

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

在衍生類別中覆寫時,從繫結堆疊的適當層次中,傳回要求的型別物件 (如果有的話)。When overridden in a derived class, returns a typed object requested, if present, from the appropriate layer in the binding stack.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於