Windows Communication Foundation(WCF)是微軟為構(gòu)建面向服務(wù)的應(yīng)用程序所提供的統(tǒng)一編程模型。
WCF的基本概念:
地址:定義服務(wù)的地址
綁定:定義服務(wù)的通訊方式(傳輸協(xié)議、編碼方案)
契約:定義服務(wù)的具體實(shí)現(xiàn)
終結(jié)點(diǎn):由地址、綁定和契約共同構(gòu)成一個(gè)終結(jié)點(diǎn),服務(wù)器通過(guò)終結(jié)點(diǎn)向客戶(hù)端公開(kāi)服務(wù),客戶(hù)端通過(guò)終結(jié)點(diǎn)調(diào)用服務(wù)。
下面通過(guò)一個(gè)簡(jiǎn)單的服務(wù)示例來(lái)認(rèn)識(shí)WCF(只需讓本例順利運(yùn)行即可,關(guān)于代碼中的各種類(lèi)型及WCF的相關(guān)概念我們將在后續(xù)介紹):
1.新建項(xiàng)目,名稱(chēng) XfrogWCFService,解決方案名稱(chēng) XfrogWCFStudy001,模板選擇類(lèi)庫(kù),選擇.NET Framework 3.0版本
2.修改Class1.cs文件名稱(chēng)為 IFirstService.cs
3.添加引用 System.ServiceModel
4.修改IFirstService.cs代碼如下:
隱藏行號(hào) 復(fù)制代碼 ? 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);
}
}
我們定義了一個(gè)IFirstService接口,注意在接口上申明了ServiceContract特性,即服務(wù)契約,表明該接口是一個(gè)服務(wù)。方法上聲明了OperationContract特性,表示該方法是IFirstService的一個(gè)服務(wù)方法,客戶(hù)端可遠(yuǎn)程調(diào)用該方法。
5.添加一個(gè)新類(lèi),文件名為FirstService.cs,代碼如下:
隱藏行號(hào) 復(fù)制代碼 ? 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,到此我們的服務(wù)代碼已經(jīng)編寫(xiě)完成,下面我們必須為服務(wù)提供一個(gè)運(yùn)行的宿主,通過(guò)該宿主程序來(lái)啟動(dòng)我們的服務(wù)。
6.在同一解決方案下新建一個(gè)項(xiàng)目,名稱(chēng)為Host,類(lèi)型為控制臺(tái)應(yīng)用程序
7.Host項(xiàng)目中添加引用,引用項(xiàng)目XfrogWCFService,然后再添加引用:System.ServiceModel
8.修改Program.cs代碼如下:
隱藏行號(hào) 復(fù)制代碼 ? 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("服務(wù)已啟動(dòng),按任意鍵中止...");
Console.ReadKey(true);
host.Close();
}
}
}
}
以上,我們已經(jīng)實(shí)現(xiàn)了服務(wù)以及為服務(wù)提供了一個(gè)運(yùn)行宿主,即契約部分已經(jīng)完成,下面我們?yōu)榉?wù)指定地址及綁定,本步驟可通過(guò)WCF的管理工具,或直接編寫(xiě)配置文件來(lái)完成。我們先采用手工編寫(xiě)配置文件的方式:
9.新建項(xiàng),選擇應(yīng)用程序配置文件,文件名App.config保持不變。
10.修改app.config內(nèi)容如下:
隱藏行號(hào) 復(fù)制代碼 ? 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.設(shè)置Host項(xiàng)目為啟動(dòng)項(xiàng)目,啟動(dòng)調(diào)試??刂婆_(tái)上顯示服務(wù)已啟動(dòng)后,打開(kāi)瀏覽器輸入服務(wù)地址:http://localhost:8000/ ,瀏覽器中會(huì)打開(kāi)我們的服務(wù)頁(yè)面,這表示我們的服務(wù)已經(jīng)啟動(dòng)成功,客戶(hù)端可通過(guò)該地址訪問(wèn)我們的服務(wù)了。
下面,我們將創(chuàng)建一個(gè)客戶(hù)端來(lái)訪問(wèn)我們的服務(wù)
12.在同一解決方案下新建一個(gè)項(xiàng)目,名稱(chēng)為Client,類(lèi)型為控制臺(tái)應(yīng)用程序
13.我們將使用微軟的svcutil工具生成FirstService服務(wù)的客戶(hù)端代理類(lèi),通過(guò)開(kāi)始菜單/Microsoft Visual Studio 2008/Visual Studio Tools/Visual Studio 2008命令提示,啟動(dòng)命令環(huán)境。
14.確認(rèn)FirstService服務(wù)已啟動(dòng)
15.切換當(dāng)前路徑到解決方案目錄:
cd G:\Study\WCF\XfrogWCFStudy001
g:
16.輸入命令:
svcutil http://localhost:8000/?wsdl /o:FirstServiceClient.cs
執(zhí)行成功后,會(huì)在解決方案目錄下生成兩個(gè)文件:FirstServiceClient.cs 和output.config
17.中止Host項(xiàng)目的調(diào)試,回到Client項(xiàng)目,選擇添加 現(xiàn)有項(xiàng) ,然后選擇這兩個(gè)文件,添加后,將output.config重命名為App.config
18.Client項(xiàng)目中添加引用,選擇System.ServiceModel
19.修改program.cs代碼如下:
隱藏行號(hào) 復(fù)制代碼 ? 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項(xiàng)目上單擊右鍵,選擇調(diào)試—>啟動(dòng)新實(shí)例,待服務(wù)啟動(dòng)完成后,在Client項(xiàng)目上單擊右鍵,選擇調(diào)試—>啟動(dòng)新實(shí)例。輸入任意字符回車(chē),Client將調(diào)用FirstService服務(wù)GetData方法,返回一個(gè)字符串。輸入q退出Client。