自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF初次操作實(shí)踐

開發(fā) 后端
本文將通過實(shí)現(xiàn)一個(gè)簡單的示例來對(duì)WCF有個(gè)直觀而淺顯的認(rèn)識(shí),希望對(duì)初次涉及WCF的朋友有所幫助。

  我們通過實(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、編寫代碼如下:

 

  1.   DataContracts.cs  
  2.   usingSystem;  
  3.   usingSystem.Runtime.Serialization;  
  4.   namespaceWCFDemo.Contracts  
  5.   {  
  6.   [DataContract]  
  7.  publicclassDemoData  
  8.   {  
  9.   [DataMember]  
  10.   publicintID { get;set;}  
  11.   [DataMember]  
  12.  publicdoubleValue { get;set;}  
  13.   [DataMember]  
  14.   publicDateTime ValueTime { get;set;}  
  15.   [DataMember]  
  16.   publicDeviceState State { get;set;}  
  17.  [DataMember]  
  18.  publicDateTime StateTime { get;set;}  
  19.  }  
  20.   publicenumDeviceState  
  21.   {  
  22.   Unknown,  
  23.   Working,  
  24.   Broken  
  25.   }  
  26.   } 

 

 ?。}外話:DemoData類中各個(gè)屬性的寫法有些偷懶,其實(shí)個(gè)人不建議這樣。這里是為了代碼簡單……)

 

  1.   ServiceContracts.cs  
  2.   usingSystem.Collections.Generic;  
  3.   usingSystem.ServiceModel;  
  4.   namespaceWCFDemo.Contracts  
  5.   {  
  6.   [ServiceContract]  
  7.   publicinterfaceIDemoService  
  8.   {  
  9.   [OperationContract]  
  10.   List<DemoData> GetMonitorData();  
  11.   }  
  12.   } 

 

  第二步:服務(wù)

  1、添加一個(gè)類庫WCFDemo.Services。

  2、在類庫中加入一個(gè)文件Services.cs用來放置實(shí)現(xiàn)服務(wù)的類。

  3、添加引用WCFDemo.Contracts。

  4、編寫代碼如下:

 

  1.   usingSystem;  
  2.   usingSystem.Collections.Generic;  
  3.   usingWCFDemo.Contracts;  
  4.   namespaceWCFDemo.Services  
  5.  {  
  6.   publicclassDemoService : IDemoService  
  7.   {  
  8.   Random random = newRandom();  
  9.   publicList<DemoData> GetMonitorData()  
  10.   {  
  11.   List<DemoData> r = newList<DemoData>();  
  12.   r.Add(newDemoData() { ID = 1, Value = random.Next(100), ValueTime = DateTime.Now, State = DeviceState.Unknown, StateTime = DateTime.Now });  
  13.   r.Add(newDemoData() { ID = 2, Value = random.Next(100), ValueTime = DateTime.Now, State = DeviceState.Working, StateTime = DateTime.Now });  
  14.   r.Add(newDemoData() { ID = 3, Value = random.Next(100), ValueTime = DateTime.Now, State = DeviceState.Broken, StateTime = DateTime.Now });  
  15.   returnr;  
  16.   }  
  17.   }  
  18.   } 

 

  (題外話:第一步時(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,并編寫代碼如下:

 

  1.   usingSystem;  
  2.   usingSystem.Windows.Forms;  
  3.   usingSystem.ServiceModel;  
  4.   usingWCFDemo.Services;  
  5.   usingWCFDemo.Contracts;  
  6.   namespaceWCFDemo.Host.WithoutConfig  
  7.   {  
  8.   publicpartialclassHostForm : Form  
  9.   {  
  10.   publicHostForm()  
  11.   {  
  12.   InitializeComponent();  
  13.   }  
  14.   ServiceHost host;  
  15.   privatevoidbutton1_Click(objectsender, EventArgs e)  
  16.   {  
  17.   host = newServiceHost(typeof(DemoService));  
  18.   host.AddServiceEndpoint(typeof(IDemoService), newBasicHttpBinding(), "http://localhost:5678/DemoService");  
  19.   host.Opened += delegate{ label1.Text = "服務(wù)啟動(dòng)";};  
  20.   host.Open();  
  21.   }  
  22.   privatevoidbutton2_Click(objectsender, EventArgs e)  
  23.   {  
  24.   if(host != null&&host.State == CommunicationState.Opened)  
  25.   {  
  26.   host.Closed += delegate{ label1.Text = "服務(wù)停止";};  
  27.   host.Close();  
  28.   }  
  29.   }  
  30.   }  
  31.   } 

 

  第四步:客戶端

  1、添加一個(gè)Windows窗體應(yīng)用程序WCFDemo.Client.WithoutConfig。

  2、添加引用System.ServiceModel。

  3、引用之前契約項(xiàng)目。

  4、在窗體放置一個(gè)Button和一個(gè)DataGridView,并編寫代碼如下:

 

  1.   usingSystem;  
  2.   usingSystem.Windows.Forms;  
  3.   usingSystem.ServiceModel;  
  4.   usingWCFDemo.Contracts;  
  5.   namespaceWCFDemo.Client.WithoutConfig  
  6.   {  
  7.   publicpartialclassClientForm : Form  
  8.   {  
  9.   publicClientForm()  
  10.   {  
  11.   InitializeComponent();  
  12.   }  
  13.   privatevoidbutton1_Click(objectsender, EventArgs e)  
  14.   {  
  15.   using(ChannelFactory<IDemoService> f = newChannelFactory<IDemoService>(newBasicHttpBinding(), "http://localhost:5678/DemoService"))  
  16.   {  
  17.   dataGridView1.DataSource = f.CreateChannel().GetMonitorData();  
  18.   }  
  19.   }  
  20.   }  
  21.   } 

 

  到這里,已經(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

【編輯推薦】

  1. 5月最新超有趣的免費(fèi)jQuery插件推薦
  2. 從零開始學(xué)習(xí)jQuery之管理jQuery包裝集
  3. jQuery性能指標(biāo)和調(diào)優(yōu)
  4. 手把手教你jQuery jqPlot畫圖插件
  5. 從零開始學(xué)習(xí)jQuery之萬能的選擇器
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2010-02-22 13:56:35

WCF服務(wù)契約

2010-03-01 15:27:35

WCF分布操作

2009-11-09 13:12:14

WCF事物操作

2009-12-22 19:06:51

WCF自承載

2010-03-01 15:08:05

WCF單向操作

2009-11-05 16:21:51

WCF服務(wù)

2009-11-06 16:35:56

WCF Stream對(duì)

2010-03-01 15:51:01

WCF限流

2010-02-26 09:33:18

WCF創(chuàng)建WebSer

2010-03-01 16:31:58

WCF實(shí)現(xiàn)SOA

2010-03-01 10:12:54

WCF異步操作

2009-12-21 15:12:40

WCF操作Stream

2010-02-23 17:59:52

WSIT連接WCF

2009-11-09 13:47:22

WCF Stream操

2010-02-22 15:13:04

WCF分布式事務(wù)

2009-12-07 14:35:42

WCF異步調(diào)用

2009-12-21 11:19:50

WCF配置文件

2010-03-02 10:54:42

WCF回調(diào)操作

2010-05-04 16:19:12

Unix命令

2010-03-02 09:39:11

保護(hù)WCF服務(wù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)