Binding 類別

定義

包含繫結項目,這些項目會指定用於用戶端與服務之通訊的通訊協定、傳輸和訊息編碼器。Contains the binding elements that specify the protocols, transports, and message encoders used for communication between clients and services.

public ref class Binding abstract : System::ServiceModel::IDefaultCommunicationTimeouts
public abstract class Binding : System.ServiceModel.IDefaultCommunicationTimeouts
type Binding = class
    interface IDefaultCommunicationTimeouts
Public MustInherit Class Binding
Implements IDefaultCommunicationTimeouts
繼承
Binding
衍生
實作

範例

BasicHttpBinding binding = new BasicHttpBinding();

binding.Name = "binding1";

Uri baseAddress = new Uri("http://localhost:8000/servicemodelsamples/service");
Uri address = new Uri("http://localhost:8000/servicemodelsamples/service/calc");

// Create a ServiceHost for the CalculatorService type and provide the base address.
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
{
    serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, address);

    // Open the ServiceHostBase to create listeners and start listening for messages.
    serviceHost.Open();

    // The service can now be accessed.
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <ENTER> to terminate service.");
    Console.WriteLine();
    Console.ReadLine();
    // Close the ServiceHostBase to shutdown the service.
    serviceHost.Close();
}

備註

表示繫結項目的集合,集合中的每個繫結項目會分別描述端點如何與其他端點進行通訊,也就是一直在用戶端的通道處理站和服務的通道接聽程式 (Listener) 內建置端點。Represents a collection of binding elements, each of which describes an aspect of how an endpoint communicates with other endpoints and that are built, consistently, into a channel factory on the client and into a channel listener on the service. 繫結包含繫結項目的集合,這些項目會對應至通訊協定通道、傳輸通道和訊息編碼器。A binding contains a collection of binding elements that correspond to protocol channels, transport channels, and message encoders. 通訊協定通道的繫結項目可以是任意數目,但是每一個傳輸和訊息編碼器只能有一個繫結項目。There can be any number of binding elements for protocol channels but one and only one binding element for each the transport and message encoder. 通常在繫結中,會有六層的繫結項目。There are commonly six layers of binding elements in a binding. 只需要堆疊底部的傳輸和編碼繫結項目。Only the transport and encoding binding elements at the bottom of the stack are required. 由於每個系結都需要編碼方式,因此,如果未指定編碼,Windows Communication Foundation (WCF)會為您新增預設編碼。Because an encoding is required for each binding, if an encoding is not specified, Windows Communication Foundation (WCF) adds a default encoding for you. 預設為 Text/XML (適用於 HTTP 和 HTTPS 傳輸) 和 Binary (適用於其他傳輸)。The default is Text/XML for the HTTP and HTTPS transports, and Binary for other transports.

下表摘要列出每一層的選項。The following table summarizes the options for each layer.

圖層Layer 選項Options 必要Required
異動流程Transaction Flow TransactionFlowBindingElement No
可靠性Reliability ReliableSessionBindingElement No
安全性Security 對稱、不對稱、傳輸層級Symmetric, Asymmetric, Transport-Level No
形狀變更Shape Change CompositeDuplexBindingElement No
傳輸升級Transport Upgrades SSL 資料流、Windows 資料流、對等解析程式SSL stream, Windows stream, Peer Resolver No
編碼Encoding 文字、二進位、MTOM、自訂Text, Binary, MTOM, Custom Yes
TransportTransport TCP、具名管道、HTTP、HTTPS、MSMQ、自訂TCP, Named Pipes, HTTP, HTTPS, MSMQ, Custom Yes

每一個繫結項目都會提供規格,以便在用戶端上建立通道處理站,並在服務上建立通道接聽程式。Each binding element provides the specification for building a channel factory on the client and a channel listener on the service. 例如,當建構通道處理站堆疊時,繫結中每一個繫結項目只會在堆疊中擁有一個通道處理站。When the channel factory stack is constructed, for example, there is one channel factory in the stack for each binding element in the binding. 相同類型的對應適用於服務上堆疊中的通道接聽程式。The same sort of mapping applies to the channel listeners in the stack on the service. 用戶端和服務上的一致性對於在這些端點之間建立通道型的連線非常重要。Consistency on the client and service is critical for establishing the channel-based connection between these endpoints. 然後,每一個處理站和接聽程式都會處理通道堆疊中與其連接之對應通道的傳送和接受作業,然後這些通道就可以傳送和接收用於通訊的訊息。Each factory and listener, in turn, processes the sending and accepting of the corresponding channels in the channel stack that connects them, and these channels can then send and receive the messages used to communicate.

Binding 的每一個執行個體都有 NameNamespace,這兩者放在一起時可在服務的中繼資料 (Metadata) 內唯一識別此執行個體。Each instance of a Binding has a Name and Namespace that together uniquely identify it in the metadata of the service. 如果未指定名稱或命名空間,WCF 會為您新增預設值。If no name or namespace is specified, WCF adds a default values for you. 預設名稱為 null,而預設命名空間為 http://tempuri.org/The default name is null and the default namespace is http://tempuri.org/. 繫結的這個使用者名稱與通訊協定名稱的規格不同,後者是由 Scheme 屬性所指定。This user name for the binding is distinct from the specification of the protocol name, which is specified by the Scheme property. 例如,如果您要新增更多 HTTP 繫結,您可以任意命名,並將其所有配置設定為 "http"。If you want to add more HTTP bindings, for example, you can name them whatever you want and set all of their schemes to "http". 沒有任何原有應用程式或機器分派是根據 SchemeThere is no inherent application or machine dispatch based on the Scheme. 因此,您可以避開無法為已知通訊協定註冊其他處理常式的常見問題。So you avoid the common problem of being unable to register additional handlers for well-known protocols. 您也可以輕鬆地並存使用多個版本的繫結,只需要為每一個版本提供不同的名稱即可。You can also easily work with multiple versions of a binding side-by-side by giving each version a different name.

Binding 類別會實作 IDefaultCommunicationTimeouts 介面來減少阻斷服務 (DOS) 攻擊情形,這類攻擊會長時間佔用資源。The Binding class implements the IDefaultCommunicationTimeouts interface to mitigate Denial of Service (DOS) attacks that rely on tying up resources for extended intervals of time. 此實作會指定通訊逾時值來開啟和關閉連接,並用於讀取和寫入與接收和傳送訊息有關的作業。The implementation specifies the communication timeouts values for opening and closing connections and for the reading and writing operations associated with receiving and sending messages. 用來取得及設定這些逾時值和其預設值之屬性的作業摘要列於下表中。The properties used to get and set these timeouts and their default values operations are summarized in the following table.

Timeout 屬性Timeout Property 預設值Default Value
OpenTimeout 1 分鐘1 minute
CloseTimeout 1 分鐘1 minute
SendTimeout 1 分鐘1 minute
ReceiveTimeout 10 分鐘10 minutes

當您從 Binding 繼承來建立繫結時,必須覆寫 CreateBindingElementsWhen you create a binding by inheriting from Binding, you must override CreateBindingElements.

此外,您也可以定義自己的繫結項目,並將其插入上表定義的任何層次之間。In addition, you can define your own binding elements and insert them between any of the defined layers in the preceding table. 如需詳細資訊,請參閱 CustomBinding 類別。For more information, see the CustomBinding class.

建構函式

Binding()

使用預設的名稱和命名空間,初始化 Binding 類別的新執行個體。Initializes a new instance of the Binding class with a default name and namespace.

Binding(String, String)

從服務之指定的繫結,初始化 Binding 類別的新執行個體。Initializes a new instance of the Binding class from a specified binding of the service.

屬性

CloseTimeout

取得或設定針對連線所提供的時間間隔 (此連線要在傳輸引發例外狀況之前關閉)。Gets or sets the interval of time provided for a connection to close before the transport raises an exception.

MessageVersion

取得用戶端所使用的訊息版本及使用繫結所設定的服務。Gets the message version used by clients and services configured with the binding.

Name

取得或設定繫結的名稱。Gets or sets the name of the binding.

Namespace

取得或設定繫結的 XML 命名空間。Gets or sets the XML namespace of the binding.

OpenTimeout

取得或設定針對連線所提供的時間間隔 (此連線要在傳輸引發例外狀況之前開啟)。Gets or sets the interval of time provided for a connection to open before the transport raises an exception.

ReceiveTimeout

取得或設定連線中斷之前,可以維持非作用狀態的時間間隔 (在此期間未接收應用程式訊息)。Gets or sets the interval of time that a connection can remain inactive, during which no application messages are received, before it is dropped.

Scheme

在衍生類別中實作時,會設定 URI 配置,此配置可指定繫結所建置之通道和接聽程式處理站所使用的傳輸。When implemented in a derived class, sets the URI scheme that specifies the transport used by the channel and listener factories that are built by the bindings.

SendTimeout

取得或設定針對寫入作業所提供的時間間隔 (此作業要在傳輸引發例外狀況之前完成)。Gets or sets the interval of time provided for a write operation to complete before the transport raises an exception.

方法

BuildChannelFactory<TChannel>(BindingParameterCollection)

在用戶端上建置通道處理站堆疊,此堆疊會建立指定的通道型別,並滿足繫結參數集合所指定的功能。Builds the channel factory stack on the client that creates a specified type of channel and that satisfies the features specified by a collection of binding parameters.

BuildChannelFactory<TChannel>(Object[])

在用戶端上建置通道處理站堆疊,此堆疊會建立指定的通道型別,並滿足物件陣列所指定的功能。Builds the channel factory stack on the client that creates a specified type of channel and that satisfies the features specified by an object array.

BuildChannelListener<TChannel>(BindingParameterCollection)

在服務上建置通道接聽程式,此接聽程式會接受指定的通道型別,並滿足繫結參數集合所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified by a collection of binding parameters.

BuildChannelListener<TChannel>(Object[])

在服務上建置通道接聽程式,此接聽程式可接受指定的通道型別並滿足指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, BindingParameterCollection)

在服務上建置通道接聽程式,此接聽程式可接受指定的通道型別並滿足指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, Object[])

在服務上建置通道接聽程式,此接聽程式可接受指定的通道型別並滿足指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection)

在服務上建置通道接聽程式,此接聽程式可接受指定的通道型別並滿足指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection)

在服務上建置通道接聽程式,此接聽程式可接受指定的通道型別並滿足指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[])

在服務上建置通道接聽程式,此接聽程式可接受指定的通道型別並滿足指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, Object[])

在服務上建置通道接聽程式,此接聽程式可接受指定的通道型別並滿足指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

CanBuildChannelFactory<TChannel>(BindingParameterCollection)

傳回值,這個值指出目前的繫結是否可以在用戶端上建置滿足指定之繫結參數集合的通道處理站堆疊。Returns a value that indicates whether the current binding can build a channel factory stack on the client that satisfies the collection of binding parameters specified.

CanBuildChannelFactory<TChannel>(Object[])

傳回值,這個值指出目前的繫結是否可以在用戶端上建置滿足物件陣列指定之需求的通道處理站堆疊。Returns a value that indicates whether the current binding can build a channel factory stack on the client that satisfies the requirements specified by an object array.

CanBuildChannelListener<TChannel>(BindingParameterCollection)

傳回值,這個值指出目前的繫結是否可以在服務上建置滿足指定之繫結參數集合的通道接聽程式堆疊。Returns a value that indicates whether the current binding can build a channel listener stack on the service that satisfies the collection of binding parameters specified.

CanBuildChannelListener<TChannel>(Object[])

傳回值,這個值指出目前的繫結是否可以在服務上建置滿足物件陣列中指定之準則的通道接聽程式堆疊。Returns a value that indicates whether the current binding can build a channel listener stack on the service that satisfies the criteria specified in an array of objects.

CreateBindingElements()

在衍生類別 (Derived Class) 中覆寫時,會建立集合,此集合中包含屬於目前繫結之一部分的繫結項目。When overridden in a derived class, creates a collection that contains the binding elements that are part of the current binding.

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

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

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

從繫結堆疊的適當層次中,傳回要求的型別物件 (如果有)。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)
ShouldSerializeName()

傳回繫結的名稱是否應該序列化。Returns whether the name of the binding should be serialized.

ShouldSerializeNamespace()

傳回繫結的命名空間是否應該序列化。Returns whether the namespace of the binding should be serialized.

ToString()

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

(繼承來源 Object)

適用於