[C#.NET][WCF] 在雲端虛機上部署我的 wsHttpBinding self-host

  • 4633
  • 0
  • WCF
  • 2013-08-16

[C#.NET][WCF] 在雲端虛機上部署我的 wsHttpBinding self-host

預設WCF Service Library是使用 wsHttpBinding ,所以我就拿它來當部署範例,上篇的專案繼續延用[WCF] 一隻簡單的 Console Application Host,在這裡我就使用變更設定檔的方式做說明。

部署Host的機器我就拿雲端上的虛機來做範例,當然你也不一定要用雲端上的虛機,事實上隨便找一台電腦就能部署了。

 

請繼續往下看。

Step1.設定雲端機器的EndPoint,關掉防火牆(偷懶做法)。

[Windows Azure] EndPoint Configuration for Virtual Machine

 

Step2.執行Edit WCF Configuration,修改設定檔。

對WcfServiceLibrary專案的App.Config按右鍵,執行Edit WCF Configuration

SNAGHTML1980c7c7

 

新增一個新的wsHttpBinding

SNAGHTML19852abd

 

為設定命名。

SNAGHTML1912ae99

 

 

 

關閉安全性功能(又是偷懶的做法?)

SNAGHTML19135d8d

 

為 EndPount 設定我們剛剛建立的wsHttpBinding設定

SNAGHTML19146e11

 

 

為Host設定跟雲端虛機上一樣的port

SNAGHTML191dac2a

 

存檔

SNAGHTML19190cdf

 

變更好的App.Config設定如下:


<?xml version="1.0" encoding="utf-8" ?>
<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>
      <wsHttpBinding>
        <binding name="wsHttpBinding.Config">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="WcfServiceLibrary.Service">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpBinding.Config"
          contract="WcfServiceLibrary.IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:168/wsHttpBinding/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes,
          set the value below to true.  Set to false before deployment
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
 
</configuration>

 

 

Step3.在本機測試Host

按下F5,測試我們的設定有沒有問題

SNAGHTML191af7ba

 

PS.在本機上測試沒問題,不代表發佈到其他電腦也會沒問題

 

Step4.利用WcfSvcHos當載體

Copy 以下檔案到雲端虛機

1.WcfServiceLibrary.dll、WcfServiceLibrary.dll.Config

2.WcfSvcHost.exe

image

 

執行WcfSvcHost - Shortcut

image

詳細的做法請參考

[WCF] 開發工具 WcfSvcHost.exe & WcfTestClient.exe

 

Step5.更新Client專案的設定

先把原本專案裡的App.Config砍掉

再對ServiceReference1設定更新

SNAGHTML191ea4e9

 

貼上新的連結,按下OK

SNAGHTML1927bc21

 

更新完的App.Conifg檔如下


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IService">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:168/wsHttpBinding/" binding="wsHttpBinding"
          bindingConfiguration="WSHttpBinding_IService" contract="ServiceReference1.IService"
          name="WSHttpBinding_IService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>


更改Client專案的設定檔,改成遠端的位址

SNAGHTML192e7523

 

PS.直接更改應用程式的Config檔的也是一樣,要注意EndPoint及Binding的設定是否跟遠端的一樣

SNAGHTML19304f6b

 

Step6.設定Client為主專案,並刪除WcfServiceLibrary專案的App.Conifg

SNAGHTML192b53fd

 

 

 

 

按下F5,並按下按鈕,跳出視窗表示已經正確呼叫 WCF Service方法。

SNAGHTML1974b678


再來用我們上個專案寫的Colsole Application當Host載體,只要將我們在WcfServiceLibrary專案改好的App.Config貼到 WcfServiceHost.exe.config

SNAGHTML19791c6b

 

 

 

然後把資料夾內必要的檔案都Copy到雲端虛機,執行WcfServiceHost.exe。阿..出現亂碼了,應該是不影響功能才是。

image

 

 

 

再次由Client呼叫,結果一樣。

SNAGHTML197d67cc

 

 

 

 

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


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

Image result for microsoft+mvp+logo