CustomBinding 類別

定義

從一串結合元素中定義綁定。

public ref class CustomBinding : System::ServiceModel::Channels::Binding
public class CustomBinding : System.ServiceModel.Channels.Binding
[System.Windows.Markup.ContentProperty("Elements")]
public class CustomBinding : System.ServiceModel.Channels.Binding
type CustomBinding = class
    inherit Binding
[<System.Windows.Markup.ContentProperty("Elements")>]
type CustomBinding = class
    inherit Binding
Public Class CustomBinding
Inherits Binding
繼承
CustomBinding
屬性

範例

以下範例展示了如何使用 CustomBinding a ReliableSessionBindingElementHttpTransportBindingElement

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

// Create a ServiceHost for the CalculatorService type and provide the base address.
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
{
    // Create a custom binding that contains two binding elements.
    ReliableSessionBindingElement reliableSession = new ReliableSessionBindingElement();
    reliableSession.Ordered = true;

    HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
    httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
    httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;

    CustomBinding binding = new CustomBinding(reliableSession, httpTransport);

    // Add an endpoint using that binding.
    serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, "");

    // Add a MEX endpoint.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.HttpGetUrl = new Uri("http://localhost:8001/servicemodelsamples");
    serviceHost.Description.Behaviors.Add(smb);

    // 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();
}
Dim baseAddress As New Uri("http://localhost:8000/servicemodelsamples/service")

' Create a ServiceHost for the CalculatorService type and provide the base address.
Using serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)
    ' Create a custom binding that contains two binding elements.
    Dim reliableSession As New ReliableSessionBindingElement()
    reliableSession.Ordered = True

    Dim httpTransport As New HttpTransportBindingElement()
    httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous
    httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard

    Dim binding As New CustomBinding(reliableSession, httpTransport)

    ' Add an endpoint using that binding.
    serviceHost.AddServiceEndpoint(GetType(ICalculator), binding, "")

    ' Add a MEX endpoint.
    Dim smb As New ServiceMetadataBehavior()
    smb.HttpGetEnabled = True
    smb.HttpGetUrl = New Uri("http://localhost:8001/servicemodelsamples")
    serviceHost.Description.Behaviors.Add(smb)

    ' 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()
End Using

備註

當系統提供的綁定不符合服務需求時,請使用自訂綁定。 例如,可以使用自訂綁定來啟用新的傳輸或服務端點的新編碼器。

自訂綁定是利用 CustomBinding 一組以特定順序「堆疊」的綁定元素之一構建的:

下表摘要說明每個圖層的選項。

層級 選項 Required
交易流程 TransactionFlowBindingElement No
Reliability ReliableSessionBindingElement No
安全性 對稱、不對稱、Transport-Level No
變形 CompositeDuplexBindingElement No
交通升級 SSL 串流、Windows 串流、對等解析器 No
編碼 Text、Binary、MTOM、Custom Yes
Transport TCP、命名管道、HTTP、HTTPS、MSMQ 的各種版本、自訂 Yes

此外,您可以定義自己的綁定項,並在上述任何定義的層次之間插入它們。

關於如何使用自訂綁定修改系統提供的綁定的討論,請參見 「如何:自訂 System-Provided 綁定」。

Note

當呼叫以 Framework 4.0 或更新版本建置的 WCF 服務時,從以 Framework 3.5 或更早版本建置的 WCF 用戶端應用程式呼叫 .NET .NET Visual Studio 所 svcutil.exe 產生的設定檔,會包含綁定設定中的有效性屬性。 此屬性不會被 .NET Framework 3.5 執行時識別,應用程式會拋出帶有「未識別屬性有效性」訊息的 ConfigurationErrorsException。 為了解決這個問題,請從綁定設定中移除有效性屬性。

建構函式

名稱 Description
CustomBinding()

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

CustomBinding(Binding)

從指定綁定值初始化該類別的新實例 CustomBinding

CustomBinding(BindingElement[])

從一組綁定元素的陣列初始化該 CustomBinding 類別的新實例。

CustomBinding(IEnumerable<BindingElement>)

初始化該類別的新實例 CustomBinding ,使用完整通道堆疊中的綁定元素。

CustomBinding(String, String, BindingElement[])

從一組結合元素陣列初始化該類別的新實例 CustomBinding ,並指定名稱與命名空間。

CustomBinding(String)

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

屬性

名稱 Description
CloseTimeout

取得或設定連接關閉的時間間隔,然後傳輸會提出例外。

(繼承來源 Binding)
Elements

從自訂綁定中取得綁定元素。

MessageVersion

透過綁定設定,取得用戶端和服務使用的訊息版本。

(繼承來源 Binding)
Name

取得或設定綁定名稱。

(繼承來源 Binding)
Namespace

取得或設定綁定的 XML 命名空間。

(繼承來源 Binding)
OpenTimeout

取得或設定連線開啟的時間間隔,然後傳輸會提出例外。

(繼承來源 Binding)
ReceiveTimeout

取得或設定連線可保持非活躍狀態(在此期間不接收任何應用程式訊息)的時間區間,然後才會被丟棄。

(繼承來源 Binding)
Scheme

取得自訂綁定所使用的 URI 傳輸方案。

SendTimeout

取得或設定寫入操作完成的時間間隔,然後傳輸會提出例外。

(繼承來源 Binding)

方法

名稱 Description
BuildChannelFactory<TChannel>(BindingParameterCollection)

在用戶端建立通道工廠堆疊,建立特定類型的通道,並滿足一組綁定參數所規定的功能。

(繼承來源 Binding)
BuildChannelFactory<TChannel>(Object[])

在用戶端建立通道工廠堆疊,建立特定類型的通道,並滿足物件陣列所指定的功能。

(繼承來源 Binding)
BuildChannelListener<TChannel>(BindingParameterCollection)

在接受特定通道類型且符合一組綁定參數所指定特徵的服務上建置通道監聽器。

(繼承來源 Binding)
BuildChannelListener<TChannel>(Object[])

在接受特定頻道類型且符合指定功能的服務上建置頻道監聽器。

(繼承來源 Binding)
BuildChannelListener<TChannel>(Uri, BindingParameterCollection)

在接受特定頻道類型且符合指定功能的服務上建置頻道監聽器。

(繼承來源 Binding)
BuildChannelListener<TChannel>(Uri, Object[])

在接受特定頻道類型且符合指定功能的服務上建置頻道監聽器。

(繼承來源 Binding)
BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection)

在接受特定頻道類型且符合指定功能的服務上建置頻道監聽器。

(繼承來源 Binding)
BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection)

在接受特定頻道類型且符合指定功能的服務上建置頻道監聽器。

(繼承來源 Binding)
BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[])

在接受特定頻道類型且符合指定功能的服務上建置頻道監聽器。

(繼承來源 Binding)
BuildChannelListener<TChannel>(Uri, String, Object[])

在接受特定頻道類型且符合指定功能的服務上建置頻道監聽器。

(繼承來源 Binding)
CanBuildChannelFactory<TChannel>(BindingParameterCollection)

回傳一個值,表示目前綁定是否能在用戶端建立符合指定綁定參數集合的通道工廠堆疊。

(繼承來源 Binding)
CanBuildChannelFactory<TChannel>(Object[])

回傳一個值,指示目前綁定是否能在用戶端建立符合物件陣列要求的通道工廠堆疊。

(繼承來源 Binding)
CanBuildChannelListener<TChannel>(BindingParameterCollection)

回傳一個值,表示目前綁定是否能在服務上建立符合指定綁定參數集合的通道監聽者堆疊。

(繼承來源 Binding)
CanBuildChannelListener<TChannel>(Object[])

回傳一個值,表示目前綁定是否能在服務上建立符合物件陣列中指定條件的通道監聽者堆疊。

(繼承來源 Binding)
CreateBindingElements()

回傳自訂綁定的綁定元素的通用集合。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

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

回傳一個從綁定堆疊中適當層請求的類型物件(若存在)。

(繼承來源 Binding)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ShouldSerializeName()

回傳綁定名稱是否應該序列化。

(繼承來源 Binding)
ShouldSerializeNamespace()

回傳綁定的命名空間是否應該序列化。

(繼承來源 Binding)
ToString()

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

(繼承來源 Object)

適用於