BasicBinding
這個範例會示範 basicHttpBinding 的使用方式,此繫結會搭配第一代和第二代 Web 服務來提供 HTTP 通訊和最大的互通性。這個範例是以實作計算機服務的使用者入門範例為基礎。
注意: |
---|
此範例的安裝程序與建置指示位於本主題的結尾。 |
用戶端和服務的組態檔中會指定繫結。此繫結型別是使用 <endpoint> 項目的 binding 屬性所指定的,如下列範例組態所示:
<services>
<service
type="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</service>
</services>
若要使用包含預設行為的基本繫結,則只需要繫結區段名稱。如果您要設定基本繫結,並變更其部分設定,則必須定義繫結組態。端點必須使用 <endpoint> 項目的 bindingConfiguration 屬性來按照名稱參考繫結組態,如下列範例程式碼所示:
<services>
<service
type="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="Binding1"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</service>
</services>
在這個範例中,繫結組態會命名為 "Binding1"
,並且如下列所定義:
<bindings>
<basicHttpBinding>
<binding name="Binding1"
hostNameComparisonMode="StrongWildcard"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
openTimeout="00:10:00"
closeTimeout="00:10:00"
maxMessageSize="65536"
maxBufferSize="65536"
maxBufferPoolSize="524288"
transferMode="Buffered"
messageEncoding="Text"
textEncoding="utf-8"
bypassProxyOnLocal="false"
useDefaultWebProxy="true" >
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
繫結項目會提供設定主機名稱比較模式、訊息大小上限、Proxy 選項、逾時、訊息編碼以及其他選項的屬性。
當您執行範例時,作業要求和回應會顯示在用戶端主控台視窗中。在用戶端視窗中按下 ENTER 鍵,即可關閉用戶端。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.
若要設定、建置及執行範例
若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例中的指示。
若要在單一或跨電腦的組態中執行本範例,請遵循執行 Windows Communication Foundation 範例中的指示。
Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.