[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
新增一個新的wsHttpBinding
為設定命名。
關閉安全性功能(又是偷懶的做法?)
為 EndPount 設定我們剛剛建立的wsHttpBinding設定
為Host設定跟雲端虛機上一樣的port
存檔
變更好的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,測試我們的設定有沒有問題
PS.在本機上測試沒問題,不代表發佈到其他電腦也會沒問題
Step4.利用WcfSvcHos當載體
Copy 以下檔案到雲端虛機
1.WcfServiceLibrary.dll、WcfServiceLibrary.dll.Config
2.WcfSvcHost.exe
執行WcfSvcHost - Shortcut
詳細的做法請參考
[WCF] 開發工具 WcfSvcHost.exe & WcfTestClient.exe
Step5.更新Client專案的設定
先把原本專案裡的App.Config砍掉
再對ServiceReference1設定更新
貼上新的連結,按下OK
更新完的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專案的設定檔,改成遠端的位址
PS.直接更改應用程式的Config檔的也是一樣,要注意EndPoint及Binding的設定是否跟遠端的一樣
Step6.設定Client為主專案,並刪除WcfServiceLibrary專案的App.Conifg
按下F5,並按下按鈕,跳出視窗表示已經正確呼叫 WCF Service方法。
再來用我們上個專案寫的Colsole Application當Host載體,只要將我們在WcfServiceLibrary專案改好的App.Config貼到 WcfServiceHost.exe.config
然後把資料夾內必要的檔案都Copy到雲端虛機,執行WcfServiceHost.exe。阿..出現亂碼了,應該是不影響功能才是。
再次由Client呼叫,結果一樣。
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET