WCF初次操作實(shí)踐
我們通過實(shí)現(xiàn)一個(gè)簡單的示例來對(duì)WCF有個(gè)直觀而淺顯的認(rèn)識(shí),希望對(duì)初次涉及WCF的朋友有所幫助。
可以簡單地認(rèn)為WCF程序分為4部分:契約、服務(wù)、宿主、客戶端。我們通過一個(gè)例子來逐步完成各部分,示例程序中,客戶端可以獲取一個(gè)信息列表,列表中每一項(xiàng)包括ID、值、讀值時(shí)刻、狀態(tài)、狀態(tài)變動(dòng)時(shí)刻。這里我用的是VS2010。
首先,創(chuàng)建一個(gè)空白解決方案WCFDemo。
我們將在其中添加n個(gè)項(xiàng)目,分別實(shí)現(xiàn)契約、服務(wù)、宿主、客戶端。如果用VS2010新建“WCF服務(wù)庫”或者“WCF服務(wù)應(yīng)用程序”,它會(huì)默認(rèn)把契約和服務(wù)放在一個(gè)項(xiàng)目中,我們這個(gè)示例把契約和服務(wù)分別放在2個(gè)類庫項(xiàng)目中。
第一步:契約
1、添加一個(gè)類庫WCFDemo.Contracts。
2、在類庫中添加2個(gè)文件DataContracts.cs和ServiceContracts.cs,分別放置數(shù)據(jù)契約和服務(wù)契約。
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類中各個(gè)屬性的寫法有些偷懶,其實(shí)個(gè)人不建議這樣。這里是為了代碼簡單……)
- ServiceContracts.cs
- usingSystem.Collections.Generic;
- usingSystem.ServiceModel;
- namespaceWCFDemo.Contracts
- {
- [ServiceContract]
- publicinterfaceIDemoService
- {
- [OperationContract]
- List<DemoData> GetMonitorData();
- }
- }
第二步:服務(wù)
1、添加一個(gè)類庫WCFDemo.Services。
2、在類庫中加入一個(gè)文件Services.cs用來放置實(shí)現(xiàn)服務(wù)的類。
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;
- }
- }
- }
(題外話:第一步時(shí)說過DemoData的偷懶寫法。如果DemoData中針對(duì)每個(gè)屬性定義私有字段,并提供帶參數(shù)的構(gòu)造函數(shù),構(gòu)造函數(shù)中對(duì)字段賦值而不是對(duì)屬性賦值,那么每個(gè)DemoData實(shí)例化時(shí)比這里的示例代碼效率高。)
到這里,服務(wù)和契約已經(jīng)完成。
剩下的就是宿主如何對(duì)外提供服務(wù)和客戶端如何享受服務(wù)了,我們先使用最最簡單的方式來實(shí)現(xiàn)。
我們先以最簡單的方式來實(shí)現(xiàn)宿主和客戶端:直接引用契約和服務(wù)項(xiàng)目、采用硬編碼的方式。
第三步:宿主
1、添加一個(gè)Windows窗體應(yīng)用程序WCFDemo.Host.WithoutConfig。
2、添加引用System.ServiceModel。
3、引用之前的兩個(gè)項(xiàng)目。
4、在窗體放置兩個(gè)Button和一個(gè)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 = "服務(wù)啟動(dòng)";};
- host.Open();
- }
- privatevoidbutton2_Click(objectsender, EventArgs e)
- {
- if(host != null&&host.State == CommunicationState.Opened)
- {
- host.Closed += delegate{ label1.Text = "服務(wù)停止";};
- host.Close();
- }
- }
- }
- }
第四步:客戶端
1、添加一個(gè)Windows窗體應(yīng)用程序WCFDemo.Client.WithoutConfig。
2、添加引用System.ServiceModel。
3、引用之前契約項(xiàng)目。
4、在窗體放置一個(gè)Button和一個(gè)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();
- }
- }
- }
- }
到這里,已經(jīng)完成了一個(gè)最簡單的WCF程序,也涉及到了WCF的基本概念:終結(jié)點(diǎn)、ABC(地址、綁定、契約)……。
這個(gè)示例很簡單(甚至簡陋,而且編碼風(fēng)格和習(xí)慣也不好 ),只是用來初識(shí)WCF,要做的還有很多。
原文鏈接:http://www.cnblogs.com/Higel/archive/2011/12/26/2301835.html
【編輯推薦】