[C#.NET][WCF] netTcpBinding @self-host

  • 6804
  • 0
  • WCF
  • 2013-10-25

[C#.NET][WCF] netTcpBinding @self-host

比起 Http協定的綁定,netTcpBinding 的綁定速度更快、更穩定,但它只能用在WCF-WCF之間的通訊,接下來就來實做這個Binding,同樣是利用上篇的專案來做演練 [WCF] 一隻簡單的 Console Application Host ,有興趣的捧油,可以照著以下步驟演練。

跟上篇一樣 [WCF] 在雲端虛機上部署我的 wsHttpBinding Host,在這裡我仍是用 Windows Azure 上的虛機演練,這次比較不同的是,我在虛機上跑 Windows 2012 並且裝了 VS2012,Host直接在開發環境測。

 

Step1.設定雲端虛機EndPoint

Port為168

詳細做法請參考

[Windows Azure] EndPoint Configuration for Virtual Machine

 

Step2.設定防火牆

還是不要偷懶比較好,就設一下吧~

image

 

Step3.設定App.Config

在WcfServiceLibrary專案裡,對App.Config按右鍵→Edit WCF Configuration

image

 

修改第一個 EndPoint 為 netTcpBinding

image

 

 

修改第二個 EndPoint 為 mexTcpBinding

image

 

將服務命名為 WcfServiceLibrary.ServiceBehavior

image

 

把 httpGetEnable 設為 false

image

 

套用設定

image

 

新增一個 Binding

image

 

選擇 netTcpBinding

image

 

將 Binding 命名為 netTcpBinding.Config

image

 

安全性設為None

image

 

套用Binding

image

 

修改Host位置為 net.tcp://localhost:168/WcfServiceLibrary

image

 

修改完成的App.Config

 


<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBinding.Config">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="WcfServiceLibrary.ServiceBehavior"
        name="WcfServiceLibrary.Service">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBinding.Config"
          contract="WcfServiceLibrary.IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:168/WcfServiceLibrary" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary.ServiceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

 

按下F5偵錯,按下Invoke測試結果是否正確

image

 

 

WcfTestClient.exe 可以檢視連線成功的 Config。

image

 

Step4.修改Client的Config

我是在本機開啟Client的專案,然後修改服務參考

SNAGHTML747a21e

貼上遠端位置

SNAGHTML7476389

 

App.Config裡的 EndPoint Address 也必須要修改成遠端位置

SNAGHTML74a31b0

 

 

 

 

 

 

Step5.按下F5測試

按下Button1

SNAGHTML75014fc

 

從雲端虛機取回結果。

SNAGHTML78de4ab

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo