共用方式為


WS 雙重 Http

雙重 Http 範例會示範如何設定 WSDualHttpBinding 繫結。 這個範例是由用戶端主控台程式 (.exe) 和網際網路資訊服務 (IIS) 所裝載的服務程式庫 (.dll) 所組成。 服務會實作雙工合約。 合約是由 ICalculatorDuplex 介面所定義,這個介面會公開數學運算作業 (加、減、乘、除)。 在此範例中,ICalculatorDuplex 介面允許用戶端執行數學運算,計算整個工作階段的執行結果。 服務會獨立地傳回 ICalculatorDuplexCallback 介面上的結果。 雙工合約需要一個工作階段,因為必須建立內容,將用戶端與服務之間傳送的訊息關聯在一起。 WSDualHttpBinding 繫結支援雙工通訊。

ms751522.note(zh-tw,VS.100).gif注意:
此範例的安裝程序與建置指示位於本主題的結尾。

ms751522.Important(zh-tw,VS.100).gif 注意:
這些範例可能已安裝在您的電腦上。 請先檢查下列 (預設) 目錄,然後再繼續。

<InstallDrive>:\WF_WCF_Samples

如果此目錄不存在,請移至用於 .NET Framework 4 的 Windows Communication Foundation (WCF) 與 Windows Workflow Foundation (WF) 範例 (英文),以下載所有 Windows Communication Foundation (WCF) 和 WF 範例。 此範例位於下列目錄。

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Binding\WS\DualHttp

若要設定使用 WSDualHttpBinding 的服務端點,請在端點組態中指定此繫結,如下所示。

<endpoint address=""
         binding="wsDualHttpBinding"
         contract="Microsoft.ServiceModel.Samples.ICalculatorDuplex" />

在用戶端上,您必須設定伺服器可用來連接用戶端的位址,如下面的範例組態中所示。

<system.serviceModel>
  <client>
    <endpoint address=
         "https://localhost/servicemodelsamples/service.svc" 
         binding="wsDualHttpBinding" 
         bindingConfiguration="Binding1" 
         contract="Microsoft.ServiceModel.Samples.ICalculatorDuplex" />
  </client>

  <bindings>
    <!-- Configure a WSDualHttpBinding that supports duplex -->
    <!-- communication. -->
    <wsDualHttpBinding>
      <binding name="Binding1"
               clientBaseAddress="https://localhost:8000/myClient/"
               useDefaultWebProxy="true"
               bypassProxyOnLocal="false">
      </binding>
    </wsDualHttpBinding>
  </bindings>
</system.serviceModel>

當您執行範例時,作業要求和回應會顯示在用戶端主控台視窗中。 在用戶端視窗中按下 ENTER 鍵,即可關閉用戶端。

Press <ENTER> to terminate client once the output is displayed.

Result(100)
Result(50)
Result(882.5)
Result(441.25)
Equation(0 + 100 - 50 * 17.65 / 2 = 441.25)

當執行範例時,您會看到訊息在回呼服務所傳送的介面時傳回到用戶端。 完成所有作業時,每個中繼結果都會顯示,並接著顯示整個方程式。 按 ENTER 鍵關閉用戶端。

若要安裝、建立及執行範例

  1. 使用下列命令安裝 ASP.NET 4.0。

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
  2. 請確定您已執行 Windows Communication Foundation 範例的單次安裝程序

  3. 若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例中的指示進行。

  4. 若要在單一或跨電腦的組態中執行本範例,請遵循Running the Windows Communication Foundation Samples中的指示。

    ms751522.Important(zh-tw,VS.100).gif 注意:
    在跨電腦組態中執行用戶端時,請確定將 endpoint 項目的 address 屬性,以及 wsDualHttpBinding 項目之 binding 項目 clientBaseAddress 屬性中的 localhost,都取代成適當電腦的名稱,如下所示:

    <client>
        <endpoint name = ""
          address=
         "http://service_machine_name/servicemodelsamples/service.svc"
        />
    </client>
    ...
    <wsDualHttpBinding>
        <binding name="DuplexBinding" clientBaseAddress=
            "http://client_machine_name:8000/myClient/">
        </binding>
    </wsDualHttpBinding>