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代碼如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.ServiceModel;
-
namespace Xfrog.Study.WCF
-
{
-
[ServiceContract]
-
public interface IFirstService
-
{
-
[OperationContract]
-
String GetData(String name);
-
}
-
}
我們定義了一個IFirstService接口,注意在接口上申明瞭ServiceContract特性,即服務契約,表明該接口是一個服務。方法上聲明瞭OperationContract特性,表示該方法是IFirstService的一個服務方法,客戶端可遠程調用該方法。
5.添加一個新類,文件名爲FirstService.cs,代碼如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
namespace Xfrog.Study.WCF
-
{
-
public class FirstService : IFirstService
-
{
-
string IFirstService.GetData(String name)
-
{
-
return String.Format("Hello {0},Welcome To WCF!", name);
-
}
-
}
-
}
OK,到此我們的服務代碼已經編寫完成,下面我們必須爲服務提供一個運行的宿主,通過該宿主程序來啓動我們的服務。
6.在同一解決方案下新建一個項目,名稱爲Host,類型爲控制檯應用程序
7.Host項目中添加引用,引用項目XfrogWCFService,然後再添加引用:System.ServiceModel
8.修改Program.cs代碼如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.ServiceModel;
-
using Xfrog.Study.WCF;
-
namespace Host
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
using (ServiceHost host = new ServiceHost(typeof(FirstService)))
-
{
-
host.Open();
-
Console.WriteLine("服務已啓動,按任意鍵中止...");
-
Console.ReadKey(true);
-
host.Close();
-
}
-
}
-
}
-
}
以上,我們已經實現了服務以及爲服務提供了一個運行宿主,即契約部分已經完成,下面我們爲服務指定地址及綁定,本步驟可通過WCF的管理工具,或直接編寫配置文件來完成。我們先採用手工編寫配置文件的方式:
9.新建項,選擇應用程序配置文件,文件名App.config保持不變。
10.修改app.config內容如下:
-
<?xml version="1.0" encoding="utf-8" ?>
-
<configuration>
-
<system.serviceModel>
-
<services>
-
<service name="Xfrog.Study.WCF.FirstService" behaviorConfiguration="behaviorConfiguration">
-
<host>
-
<baseAddresses>
-
<add baseAddress="http://localhost:8000/"/>
-
</baseAddresses>
-
</host>
-
<endpoint address="" binding="basicHttpBinding" contract="Xfrog.Study.WCF.IFirstService"></endpoint>
-
</service>
-
</services>
-
<behaviors>
-
<serviceBehaviors>
-
<behavior name="behaviorConfiguration">
-
<serviceMetadata httpGetEnabled="true"/>
-
</behavior>
-
</serviceBehaviors>
-
</behaviors>
-
</system.serviceModel>
-
</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代碼如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
namespace Client
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String key = "";
-
while (String.Compare(key, "Q", true)!=0)
-
{
-
FirstServiceClient client = new FirstServiceClient();
-
Console.WriteLine(client.GetData(key));
-
key = Console.ReadLine();
-
}
-
}
-
}
-
}
20.Host項目上單擊右鍵,選擇調試—>啓動新實例,待服務啓動完成後,在Client項目上單擊右鍵,選擇調試—>啓動新實例。輸入任意字符回車,Client將調用FirstService服務GetData方法,返回一個字符串。輸入q退出Client。