什麼是WCF:
WCF是使用托管代碼建立和運行面向服務(Service Oriented)應用程序的統 一框架。
WCF能夠建立一個跨平台的安全、可信賴、事務性的解決方案,且能與已有系 統兼容協作。
WCF是微軟分布式應用程序開發的集大成者,它整合了.Net平台下所有的和分 布式系統有關的技術,例如.Net Remoting、ASMX、WSE和MSMQ。以通信 (Communiation)范圍而論,它可以跨進程、跨機器、跨子網、企業網乃至於 Internet。
WCF可以運行在ASP.NET,EXE,WPF,Windows Forms,NT Service,COM+上面 。
WCF支持的協議包括TCP,HTTP,跨進程以及自定義,安全模式則包括SAML, Kerberos,X509,用戶/密碼,自定義等多種標准與模式。
也就是說,在WCF框架下,開發分布式系統變得容易了,微軟將所有與此相關 的技術要素都包含在內。
WCF的優勢:
統一性:
WCF是對於ASMX,.Net Remoting,Enterprise Service,WSE,MSMQ等技術的 整合。
互操作性:
於WCF最基本的通信機制是SOAP,這就保證了系統之間的互操作性,即使是運 行不同的上下文中。
可以跨進程、跨機器甚至於跨平台的通信,只要支持標准的Web Service,例 如J2EE應用服務器(如WebSphere,WebLogic)。應用程序可以運行在Windows操 作系統下,也可以運行在其他的操作系統,如Sun Solaris,HP Unix,Linux等等 。
安全與可信賴:
WS-Security,WS-Trust和WS-SecureConversation均被添加到SOAP消息中,以 用於用戶認證,數據完整性驗證,數據隱私等多種安全因素。
兼容性:
WCF充分的考慮到了與舊有系統的兼容性。安裝WCF並不會影響原有的技術如 ASMX和.Net Remoting。
WCF簡單實現:
WCF管理的是服務器端與客戶端的通信,因此要完成基本結構需要構建服務器 端和客戶端。
1.首先構建WCF服務端,包括Host(宿主)-HostMain,契約(接口)-IWCFService ,實現-WCFService。
1.1 編寫接口IWCFService,在接口中標注[ServiceContract](類)和 [OperationContract]屬性(方法)。
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace WCFHelloWorld
{
[ServiceContract]
interface IWCFService
{
[OperationContract]
void SayHello();
}
}
1.2 編寫WCFService類繼承IWCFService,由於已經在接口中標注屬性,所以 在這裡就不用寫了。
using System;
using System.Collections.Generic;
using System.Text;
namespace WCFHelloWorld
{
class WCFService: IWCFService
{
#region IWCFService 成員
public void SayHello()
{
Console.WriteLine("Hello ,You!");
}
#endregion
}
}
1.3 一個WCFService必須要有Host(宿主)作為他的運行環境,ASP.net、 WinForm、cmd程序都可以成為宿主,這裡的示例我們用控制台應用程序來作為 Host。

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace WCFHelloWorld
{
public class HostMain
{
static void Main(string[] args)
{
//創建一個Host對象
ServiceHost host = new ServiceHost(typeof
(WCFService), new Uri("http://localhost:8080/HelloToYou"));
host.AddServiceEndpoint(typeof(IWCFService), new
BasicHttpBinding(), "Svc");
host.Open();
Console.WriteLine("I'am Here.");
Console.ReadKey();
host.Close();
}
}
}
1.4 啟動調試,成功,將localhost:8080/HelloToYou輸入浏覽器地址,出 錯提示“當前已禁用此服務的元數據發布”。

1.5 解決方法:將生成頁面最下面的示例配置文件代碼拷貝後,項目新建配置 文件App.config,將代碼拷貝到App.config的<configuration>節點下。修 改<services>節點下“service name="MyNamespace.MyServiceType"”為 “service name="WCFHelloWorld.WCFService"”,保持 “behaviorConfiguration”屬性與<behaviors>節點下的 “behaviorConfiguration”屬性的內容相同,再次運行。

1.6 成功。
2.構建WCF客戶端,包括Client對象、服務器代理類。
2.1 首先啟動調試服務器端。
2.2 在 C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin下找到 svcutil.exe。
2.3運行cmd,輸入" CD C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin "
2.4 輸入 "svcutil.exe http://localhost:8080/HelloToYou"
2.5 Cmd提示在C:"Program Files"Microsoft SDKs"Windows"v6.0A"Bin下生成 兩個文件

2.6 創建WCFClient項目,將之前生成的兩個文件包含到WCFClient項目中,重 命名output.config為app.config。
2.7 添加WCFClientApp.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace WCFClient
{
public class WCFClientApp
{
static void Main(string[] args)
{
using( WCFServiceClient clientProxy = new
WCFServiceClient() )
{
clientProxy.SayHello();
}
Console.ReadKey();
}
}
}
編譯成功。
2.8 運行服務器端,然後運行客戶端。

到這裡,我們就創建了一個簡單的WCF服務。
感謝《 Windows Communication Foundation 入門 》這篇文章。