CustomBinding 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從一串結合元素中定義綁定。
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 a ReliableSessionBindingElement 和 HttpTransportBindingElement
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 一組以特定順序「堆疊」的綁定元素之一構建的:
頂端有一個可選 TransactionFlowBindingElement 選項,允許交易流動。
接下來是可選 ReliableSessionBindingElement 的,提供 WS-ReliableMessaging 規範中定義的會話與排序機制。 這種會話概念可以跨越 SOAP 與傳輸中介。
接著是一個可選的安全綁定元件,提供授權、認證、保護及保密等安全功能。 以下安全綁定元素由 Windows 通訊基金會(WCF)提供:
接下來是由綁定元素指定的可選訊息模式:
接下來是可選的運輸升級/輔助綁定元素:
接下來是必要的訊息編碼綁定元素。 你可以使用自己的傳輸工具,或使用以下其中一種訊息編碼綁定:
底部是必需的傳輸元素。 你可以使用自己的傳輸工具,或使用 Windows 通訊基金會(WCF)提供的傳輸綁定元件之一:
下表摘要說明每個圖層的選項。
| 層級 | 選項 | 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) |