WCF編程系列(一)初識WCF

Windows Communication Foundation(WCF)是微軟爲構建面向服務的應用程序所提供的統一編程模型。
WCF的基本概念:
  地址:定義服務的地址
  綁定:定義服務的通訊方式(傳輸協議、編碼方案)
  契約:定義服務的具體實現
  終結點:由地址、綁定和契約共同構成一個終結點,服務器通過終結點向客戶端公開服務,客戶端通過終結點調用服務。

下面通過一個簡單的服務示例來認識WCF(只需讓本例順利運行即可,關於代碼中的各種類型及WCF的相關概念我們將在後續介紹):
1.新建項目,名稱 XfrogWCFService,解決方案名稱 XfrogWCFStudy001,模板選擇類庫,選擇.NET Framework 3.0版本
2.修改Class1.cs文件名稱爲 IFirstService.cs
3.添加引用 System.ServiceModel
4.修改IFirstService.cs代碼如下:

隱藏行號 複製代碼 IFirstService.cs
  1. using System;
    
  2. using System.Collections.Generic;
    
  3. using System.Text;
    
  4. using System.ServiceModel;
    

  5. namespace Xfrog.Study.WCF
    
  6. {
    
  7.     [ServiceContract]
    
  8.     public interface IFirstService
    
  9.     {
    
  10.         [OperationContract]
    
  11.         String GetData(String name);
    
  12.     }
    
  13. }
    

 

我們定義了一個IFirstService接口,注意在接口上申明瞭ServiceContract特性,即服務契約,表明該接口是一個服務。方法上聲明瞭OperationContract特性,表示該方法是IFirstService的一個服務方法,客戶端可遠程調用該方法。

5.添加一個新類,文件名爲FirstService.cs,代碼如下:

隱藏行號 複製代碼 FirstService.cs
  1. using System;
    
  2. using System.Collections.Generic;
    
  3. using System.Text;
    

  4. namespace Xfrog.Study.WCF
    
  5. {
    
  6.     public class FirstService : IFirstService
    
  7.     {
    
  8.         string IFirstService.GetData(String name)
    
  9.         {
    
  10.             return String.Format("Hello {0},Welcome To WCF!", name);
    
  11.         }
    
  12.     }
    
  13. }
    

 

OK,到此我們的服務代碼已經編寫完成,下面我們必須爲服務提供一個運行的宿主,通過該宿主程序來啓動我們的服務。
6.在同一解決方案下新建一個項目,名稱爲Host,類型爲控制檯應用程序
7.Host項目中添加引用,引用項目XfrogWCFService,然後再添加引用:System.ServiceModel
8.修改Program.cs代碼如下:

隱藏行號 複製代碼 Program.cs
  1. using System;
    
  2. using System.Collections.Generic;
    
  3. using System.Text;
    
  4. using System.ServiceModel;
    
  5. using Xfrog.Study.WCF;
    

  6. namespace Host
    
  7. {
    
  8.     class Program
    
  9.     {
    
  10.         static void Main(string[] args)
    
  11.         {
    
  12.             using (ServiceHost host = new ServiceHost(typeof(FirstService)))
    
  13.             {
    
  14.                 host.Open();
    
  15.                 Console.WriteLine("服務已啓動,按任意鍵中止...");
    
  16.                 Console.ReadKey(true);
    
  17.                 host.Close();
    
  18.             }
    
  19.         }
    
  20.     }
    
  21. }
    

 


以上,我們已經實現了服務以及爲服務提供了一個運行宿主,即契約部分已經完成,下面我們爲服務指定地址及綁定,本步驟可通過WCF的管理工具,或直接編寫配置文件來完成。我們先採用手工編寫配置文件的方式:
9.新建項,選擇應用程序配置文件,文件名App.config保持不變。
10.修改app.config內容如下:

隱藏行號 複製代碼 App.config
  1. <?xml version="1.0" encoding="utf-8" ?>
    
  2. <configuration>
    
  3.   <system.serviceModel>
    
  4.     <services>
    
  5.       <service name="Xfrog.Study.WCF.FirstService" behaviorConfiguration="behaviorConfiguration">
    
  6.         <host>
    
  7.           <baseAddresses>
    
  8.             <add baseAddress="http://localhost:8000/"/>
    
  9.           </baseAddresses>
    
  10.         </host>
    
  11.         <endpoint address="" binding="basicHttpBinding" contract="Xfrog.Study.WCF.IFirstService"></endpoint>
    
  12.       </service>
    
  13.     </services>
    
  14.     <behaviors>
    
  15.       <serviceBehaviors>
    
  16.         <behavior name="behaviorConfiguration">
    
  17.           <serviceMetadata httpGetEnabled="true"/>
    
  18.         </behavior>
    
  19.       </serviceBehaviors>
    
  20.     </behaviors>
    
  21.   </system.serviceModel>
    
  22. </configuration>
    

 


11.設置Host項目爲啓動項目,啓動調試。控制檯上顯示服務已啓動後,打開瀏覽器輸入服務地址:http://localhost:8000/ ,瀏覽器中會打開我們的服務頁面,這表示我們的服務已經啓動成功,客戶端可通過該地址訪問我們的服務了。
下面,我們將創建一個客戶端來訪問我們的服務
12.在同一解決方案下新建一個項目,名稱爲Client,類型爲控制檯應用程序
13.我們將使用微軟的svcutil工具生成FirstService服務的客戶端代理類,通過開始菜單/Microsoft Visual Studio 2008/Visual Studio Tools/Visual Studio 2008命令提示,啓動命令環境。
14.確認FirstService服務已啓動
15.切換當前路徑到解決方案目錄:
cd G:\Study\WCF\XfrogWCFStudy001
g:
16.輸入命令:
svcutil http://localhost:8000/?wsdl /o:FirstServiceClient.cs
執行成功後,會在解決方案目錄下生成兩個文件:FirstServiceClient.cs 和output.config
17.中止Host項目的調試,回到Client項目,選擇添加 現有項 ,然後選擇這兩個文件,添加後,將output.config重命名爲App.config
18.Client項目中添加引用,選擇System.ServiceModel
19.修改program.cs代碼如下:

隱藏行號 複製代碼 Program.cs
  1. using System;
    
  2. using System.Collections.Generic;
    
  3. using System.Text;
    

  4. namespace Client
    
  5. {
    
  6.     class Program
    
  7.     {
    
  8.         static void Main(string[] args)
    
  9.         {
    
  10.             String key = "";
    
  11.             while (String.Compare(key, "Q", true)!=0)
    
  12.             {
    
  13.                 FirstServiceClient client = new FirstServiceClient();
    
  14.                 Console.WriteLine(client.GetData(key));
    

  15.                 key = Console.ReadLine();
    
  16.             }
    
  17.         }
    
  18.     }
    
  19. }
    

 


20.Host項目上單擊右鍵,選擇調試—>啓動新實例,待服務啓動完成後,在Client項目上單擊右鍵,選擇調試—>啓動新實例。輸入任意字符回車,Client將調用FirstService服務GetData方法,返回一個字符串。輸入q退出Client。

源碼下載


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章