在IIS中運行服務
·將項目構建到\bin目錄中
為了方便部署,我們需要對服務項目進行配置,讓它編譯到一個bin目錄中。
1. 在Solution Explorer中右鍵單擊DerivativesCalculatorService項目並選擇Properties菜單項。
2. 在Project designer中,單擊Build選項卡。
3. 將Output path從bin\Debug\改為bin\,如圖所示。
Project designer中經過調整的Output path屬性
4. 選擇File | Save All菜單項。
5. 選擇File | Close菜單項來關閉Project designer。
現在,在構建服務時產生的所有文件都會被輸出到\bin目錄中。
·添加一個.svc文件
為了讓WCF服務能夠在IIS中運行,我們需要用一種特殊的內容文件(.svc文件)表示它。這種模型和ASMX頁面在IIS中的表示方法類似。.svc文件包含一個WCF專用的處理指示符(@ServiceHost),這個指示符告訴WCF運行庫在收到消息時激活服務。
1. 在Solution Explorer中右鍵單擊DerivativesCalculatorService項目並選擇Add | New Item菜單項。
2. 在Add New Item對話框中,選擇Text File模板。
3. 在Name文本框中輸入Service.svc。
4. Add New Item對話框看起來應該如圖所示。
添加一個svc文件
5. 單擊Add按鈕。
6. 在Service.svc文件中添加下面這行語句。
<%@ServiceHost Service="DerivativesCalculatorService.Calculator" %>
7. 選擇File | Save All菜單項。
8. 選擇File | Close菜單項。
·在IIS中創建一個Web應用程序
為了方便,我們要在IIS中添加一個虛擬目錄,讓它指向DerivativesCalculatorService項目所在的目錄。這樣我們的服務程序集就能夠作為IIS應用程序運行了。
在IIS中添加Web應用程序
1. 選擇Windows的Start | Administrative Tools | Internet Information Services (IIS) Manager菜單項。
2. 在左邊的Connections部分展開樹控件,直到看到Default Web Site節點為止,如圖所示。
Default Web Site節點
3. 右鍵單擊Default Web Site節點並選擇Add Application菜單項。
Add Application對話框會彈出。
4. 在Alias文本框中輸入:
DerivativesCalculator.
5. 在Physical Path文本框中輸入:
C:\Labs\WCF-Intro\CSharp\before\DerivativesCalculator\DerivativesCalculatorService
6. 現在Add Application對話框看起來應該如圖所示。
Add Application對話框
7. 單擊OK按鈕。
驗證新的IIS應用程序
8. 在IIS Manager窗口中,右鍵單擊DerivativesCalculator節點並選擇Switch to Content View菜單項,如圖所示。
從IIS View切換到Content View
9. IIS Manager的右邊現在應該顯示出DerivativesCalculatorService的內容,如圖所示。
Content View
10. 關閉IIS Manager。
·配置服務
1. 回到Visual Studio,在Solution Explorer中右鍵單擊DerivativesCalculatorService項目並選擇Add | New Item菜單項。
2. 在Add New Item對話框的Categories列表中選擇General。在Templates部分選擇Application Configuration File模板。
3. 將文件命名為Web.Config並單擊Add按鈕。
4. 在Solution Explorer中選擇剛添加的Web.config文件,在Properties窗口中將Copy to Output屬性設為Copy always。
5. 用下面的XML代碼替換掉Web.config文件中的內容。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="DerivativesCalculatorService.Calculator">
<endpoint
address=""
binding="basicHttpBinding"
contract="DerivativesCalculatorService.IDerivativesCalculator"/>
</service>
</services>
</system.serviceModel>
</configuration>
6. 選擇File | Save All菜單項。
7. 選擇Build | Build Solution菜單項。
確認服務已經運行
8. 在Windows的開始菜單中選擇Start | Run菜單項。
9. 輸入http://localhost/DerivativesCalculator/Service.svc。
10. 按Enter鍵。
11. 瀏覽器會啟動並顯示如圖所示的Service Information頁面。
顯示在Internet Explorer 7中的Service Information頁面
12. 完成之后,關閉Internet Explorer。
·運行客戶程序來使用運行在IIS中的服務
我們可以用同一個客戶程序來調用運行在IIS中的服務。當然服務所在的終結點的位置與先前我們在客戶程序中配置的終結點的位置不同,因此需要修改。
配置客戶程序
1. 回到Visual Studio,在Solution Explorer的Client項目中右鍵單擊app.config文件並選擇Open菜單項。
2. 將文件中的endpoint的address attribute改成下面的地址:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
. . .
</bindings>
<client>
<endpoint address="http://localhost/DerivativesCalculator/Service.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IDerivativesCalculator"
contract="IDerivativesCalculator"
name="DerivativesCalculatorConfiguration" />
</client>
</system.serviceModel>
</configuration>
3. 選擇Build | Build Solution菜單項。
運行客戶程序
4. 在Solution Explorer中右鍵單擊Client項目並選擇Debug | Start new instance菜單項。
5. 在剛打開的Client.EXE命令行窗口中按Enter鍵。
客戶程序從運行在IIS中的Derivatives Calculator服務獲得了一個衍生產品的估計價格,這和我們上一次調用運行在命令行應用程序中的服務完全相同。
6. 在Client.EXE命令行窗口中按Enter鍵來關閉客戶程序。