WCF 學習筆記
Kagula
2012-2-13
正文
本文記錄學習中碰到的最常用WCF使用方式。
使用WCF目的:
解決Silverlight程式同伺服器程式高負載下的網絡通訊問題。
之是以使用WCF方式是出于以下兩個原因
[1]原來是想使用Active MQ産品,但是,支援Silverlight的Active MQ用戶端SDK還不夠成熟。
[2]直接自己在TCP上作兩層協定(一層是資料完整性協定、剩下一層做資料安全性協定),或則自己在Silverlight上實作JMS用戶端架構,但是工作量太大。
學習環境:
Win7(64位) 、 VS2010SP1 with C#
WCF簡介:
WCF(Windows Communication Foundation)是Microsoft提出的網絡通訊架構。承載WCF服務的程序稱為宿主,宿主可以是Win32 Console程式,也可以是IIS。
我的第一個WCF服務程式
在VS2010SP1内,建立C#的Win32 Console項目。第一個項目由ICalculator.cs,calculatorService.cs,App.config,program.cs四個檔案組成。
第一步:定義服務契約(Service Contract)
在項目裡引入.NET的System.ServiceMode程式集,WCF架構的絕大部分實作和API定義在該程式集中。
定義服務契約的源碼(ICalculator.cs)如下:
using System;
using System.ServiceModel;
namespace testWCF
{
[ServiceContract(Name="CalculatorService", Namespace="WCFDEMO")]
public interface ICalculator
{
[OperationContract]
double Add(double x, double y);
[OperationContract]
double Subtract(double x, double y);
[OperationContract]
double Multiply(double x, double y);
[OperationContract]
double Divide(double x, double y);
}
}
第二步:實作服務
CalculatorService.cs源碼清單如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testWCF
{
class CalculatorService:ICalculator
{
public double Add(double x, double y)
{
return x + y;
}
public double Subtract(double x, double y)
{
return x - y;
}
public double Multiply(double x, double y)
{
return x * y;
}
public double Divide(double x, double y)
{
return x / y;
}
}
}
第三步:添加App.config檔案
建立模闆,可以右鍵單擊項目名稱,添加[Windows C# Items]->[General]->[Application Configuration File]添加App.config檔案。
建立App.config目的,是定義哪些服務接口可供外部調用,接口綁定方式,源碼如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8085/calculatorservice/metadata" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name="testWCF.CalculatorService">
<endpoint address="http://127.0.0.1:8085/calculatorservice" binding="wsHttpBinding" contract="testWCF.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>
第四步:在項目的主函數裡填入Hosting代碼
啟動服務
program.cs源碼清單如下
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace testWCF
{
class Program
{
//在Win7下必須使用下面的指令授權
//netsh http add urlacl url=http://+:8085/calculatorservice user=kagula-pc\kagula
//kagula-pc:是我機器的名字
//kagula:是我機器中的預設使用者名
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Opened += delegate
{
Console.WriteLine("CalculaorService已經啟動,按任意鍵終止服務!");
};
host.Open();
Console.Read();
}
}
}
}
在Win7下你需要參考下面的控制台指令,給WCF服務程式的endpoint授權。
“netsh http add urlacl url=http://+:8085/calculatorservice user=kagula-pc\kagula”
其中“kagula-pc”是我機器的名稱,“kagula”是我在OS中的預設使用者名。
我的第一個WCF用戶端程式
建立Win32 Console應用程式
第一步:引入上文中的“ICalculator.cs”檔案
第二步:為引用WCF服務建立App.config檔案源碼如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://127.0.0.1:8085/calculatorservice" binding="wsHttpBinding" contract="testWCF.ICalculator" name="calculatorservice" />
</client>
</system.serviceModel>
</configuration>
第三步:修改program.cs檔案,調用WCF服務,源碼如下
using System;
using System.Collections.Generic;
using System.ServiceModel;
using testWCF;
namespace testWCFClient2
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>( "calculatorservice"))
{
ICalculator proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
}
}
}
}
}
現在第一對WCF服務端、用戶端程式可以運作了。
參考資料
[1]《我的WCF之旅(1):建立一個簡單的WCF程式》
http://www.cnblogs.com/artech/archive/2007/02/26/656901.html
[2]《使用WCF的相關問題》
http://www.iteye.com/blogs/tag/WCF
[3]《silverlight 連接配接wcf服務使用》
http://hi.baidu.com/mldark/blog/item/99d0fccec7266d2af9dc618c.html
[4]《如何使用 WCF 服務透過 TCP 傳輸在 Microsoft Silverlight 4》
http://support.microsoft.com/kb/2425652/zh-tw