WCF初次操作實踐
我們通過實現(xiàn)一個簡單的示例來對WCF有個直觀而淺顯的認識,希望對初次涉及WCF的朋友有所幫助。
可以簡單地認為WCF程序分為4部分:契約、服務、宿主、客戶端。我們通過一個例子來逐步完成各部分,示例程序中,客戶端可以獲取一個信息列表,列表中每一項包括ID、值、讀值時刻、狀態(tài)、狀態(tài)變動時刻。這里我用的是VS2010。
首先,創(chuàng)建一個空白解決方案WCFDemo。
我們將在其中添加n個項目,分別實現(xiàn)契約、服務、宿主、客戶端。如果用VS2010新建“WCF服務庫”或者“WCF服務應用程序”,它會默認把契約和服務放在一個項目中,我們這個示例把契約和服務分別放在2個類庫項目中。
第一步:契約
1、添加一個類庫WCFDemo.Contracts。
2、在類庫中添加2個文件DataContracts.cs和ServiceContracts.cs,分別放置數(shù)據(jù)契約和服務契約。
3、添加引用System.Runtime.Serialization和System.ServiceModel。
4、編寫代碼如下:
- DataContracts.cs
 - usingSystem;
 - usingSystem.Runtime.Serialization;
 - namespaceWCFDemo.Contracts
 - {
 - [DataContract]
 - publicclassDemoData
 - {
 - [DataMember]
 - publicintID { get;set;}
 - [DataMember]
 - publicdoubleValue { get;set;}
 - [DataMember]
 - publicDateTime ValueTime { get;set;}
 - [DataMember]
 - publicDeviceState State { get;set;}
 - [DataMember]
 - publicDateTime StateTime { get;set;}
 - }
 - publicenumDeviceState
 - {
 - Unknown,
 - Working,
 - Broken
 - }
 - }
 
(題外話:DemoData類中各個屬性的寫法有些偷懶,其實個人不建議這樣。這里是為了代碼簡單……)
- ServiceContracts.cs
 - usingSystem.Collections.Generic;
 - usingSystem.ServiceModel;
 - namespaceWCFDemo.Contracts
 - {
 - [ServiceContract]
 - publicinterfaceIDemoService
 - {
 - [OperationContract]
 - List<DemoData> GetMonitorData();
 - }
 - }
 
第二步:服務
1、添加一個類庫WCFDemo.Services。
2、在類庫中加入一個文件Services.cs用來放置實現(xiàn)服務的類。
3、添加引用WCFDemo.Contracts。
4、編寫代碼如下:
- usingSystem;
 - usingSystem.Collections.Generic;
 - usingWCFDemo.Contracts;
 - namespaceWCFDemo.Services
 - {
 - publicclassDemoService : IDemoService
 - {
 - Random random = newRandom();
 - publicList<DemoData> GetMonitorData()
 - {
 - List<DemoData> r = newList<DemoData>();
 - r.Add(newDemoData() { ID = 1, Value = random.Next(100), ValueTime = DateTime.Now, State = DeviceState.Unknown, StateTime = DateTime.Now });
 - r.Add(newDemoData() { ID = 2, Value = random.Next(100), ValueTime = DateTime.Now, State = DeviceState.Working, StateTime = DateTime.Now });
 - r.Add(newDemoData() { ID = 3, Value = random.Next(100), ValueTime = DateTime.Now, State = DeviceState.Broken, StateTime = DateTime.Now });
 - returnr;
 - }
 - }
 - }
 
?。}外話:第一步時說過DemoData的偷懶寫法。如果DemoData中針對每個屬性定義私有字段,并提供帶參數(shù)的構造函數(shù),構造函數(shù)中對字段賦值而不是對屬性賦值,那么每個DemoData實例化時比這里的示例代碼效率高。)
到這里,服務和契約已經完成。

剩下的就是宿主如何對外提供服務和客戶端如何享受服務了,我們先使用最最簡單的方式來實現(xiàn)。
我們先以最簡單的方式來實現(xiàn)宿主和客戶端:直接引用契約和服務項目、采用硬編碼的方式。
第三步:宿主
1、添加一個Windows窗體應用程序WCFDemo.Host.WithoutConfig。
2、添加引用System.ServiceModel。
3、引用之前的兩個項目。
4、在窗體放置兩個Button和一個Label,并編寫代碼如下:
- usingSystem;
 - usingSystem.Windows.Forms;
 - usingSystem.ServiceModel;
 - usingWCFDemo.Services;
 - usingWCFDemo.Contracts;
 - namespaceWCFDemo.Host.WithoutConfig
 - {
 - publicpartialclassHostForm : Form
 - {
 - publicHostForm()
 - {
 - InitializeComponent();
 - }
 - ServiceHost host;
 - privatevoidbutton1_Click(objectsender, EventArgs e)
 - {
 - host = newServiceHost(typeof(DemoService));
 - host.AddServiceEndpoint(typeof(IDemoService), newBasicHttpBinding(), "http://localhost:5678/DemoService");
 - host.Opened += delegate{ label1.Text = "服務啟動";};
 - host.Open();
 - }
 - privatevoidbutton2_Click(objectsender, EventArgs e)
 - {
 - if(host != null&&host.State == CommunicationState.Opened)
 - {
 - host.Closed += delegate{ label1.Text = "服務停止";};
 - host.Close();
 - }
 - }
 - }
 - }
 
第四步:客戶端
1、添加一個Windows窗體應用程序WCFDemo.Client.WithoutConfig。
2、添加引用System.ServiceModel。
3、引用之前契約項目。
4、在窗體放置一個Button和一個DataGridView,并編寫代碼如下:
- usingSystem;
 - usingSystem.Windows.Forms;
 - usingSystem.ServiceModel;
 - usingWCFDemo.Contracts;
 - namespaceWCFDemo.Client.WithoutConfig
 - {
 - publicpartialclassClientForm : Form
 - {
 - publicClientForm()
 - {
 - InitializeComponent();
 - }
 - privatevoidbutton1_Click(objectsender, EventArgs e)
 - {
 - using(ChannelFactory<IDemoService> f = newChannelFactory<IDemoService>(newBasicHttpBinding(), "http://localhost:5678/DemoService"))
 - {
 - dataGridView1.DataSource = f.CreateChannel().GetMonitorData();
 - }
 - }
 - }
 - }
 
到這里,已經完成了一個最簡單的WCF程序,也涉及到了WCF的基本概念:終結點、ABC(地址、綁定、契約)……。
這個示例很簡單(甚至簡陋,而且編碼風格和習慣也不好 ),只是用來初識WCF,要做的還有很多。
原文鏈接:http://www.cnblogs.com/Higel/archive/2011/12/26/2301835.html
【編輯推薦】















 
 
 
 
 
 
 