偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

WCF初次操作實踐

開發(fā) 后端
本文將通過實現(xiàn)一個簡單的示例來對WCF有個直觀而淺顯的認識,希望對初次涉及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、編寫代碼如下:

 

  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類中各個屬性的寫法有些偷懶,其實個人不建議這樣。這里是為了代碼簡單……)

 

  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.   } 

 

  第二步:服務

  1、添加一個類庫WCFDemo.Services。

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

  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.   } 

 

 ?。}外話:第一步時說過DemoData的偷懶寫法。如果DemoData中針對每個屬性定義私有字段,并提供帶參數(shù)的構造函數(shù),構造函數(shù)中對字段賦值而不是對屬性賦值,那么每個DemoData實例化時比這里的示例代碼效率高。)

  到這里,服務和契約已經完成。

  剩下的就是宿主如何對外提供服務和客戶端如何享受服務了,我們先使用最最簡單的方式來實現(xiàn)。

  我們先以最簡單的方式來實現(xiàn)宿主和客戶端:直接引用契約和服務項目、采用硬編碼的方式。

  第三步:宿主

  1、添加一個Windows窗體應用程序WCFDemo.Host.WithoutConfig。

  2、添加引用System.ServiceModel。

  3、引用之前的兩個項目。

  4、在窗體放置兩個Button和一個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 = "服務啟動";};  
  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 = "服務停止";};  
  27.   host.Close();  
  28.   }  
  29.   }  
  30.   }  
  31.   } 

 

  第四步:客戶端

  1、添加一個Windows窗體應用程序WCFDemo.Client.WithoutConfig。

  2、添加引用System.ServiceModel。

  3、引用之前契約項目。

  4、在窗體放置一個Button和一個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.   } 

 

  到這里,已經完成了一個最簡單的WCF程序,也涉及到了WCF的基本概念:終結點、ABC(地址、綁定、契約)……。

  這個示例很簡單(甚至簡陋,而且編碼風格和習慣也不好 ),只是用來初識WCF,要做的還有很多。

原文鏈接:http://www.cnblogs.com/Higel/archive/2011/12/26/2301835.html

【編輯推薦】

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

2010-02-22 13:56:35

WCF服務契約

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單向操作

2010-03-01 15:51:01

WCF限流

2010-03-01 16:31:58

WCF實現(xiàn)SOA

2009-11-06 16:35:56

WCF Stream對

2009-11-05 16:21:51

WCF服務

2010-02-26 09:33:18

WCF創(chuàng)建WebSer

2009-11-09 13:47:22

WCF Stream操

2009-12-21 15:12:40

WCF操作Stream

2010-02-23 17:59:52

WSIT連接WCF

2010-03-01 10:12:54

WCF異步操作

2010-02-22 15:13:04

WCF分布式事務

2009-12-07 14:35:42

WCF異步調用

2009-12-21 11:19:50

WCF配置文件

2010-03-02 10:54:42

WCF回調操作

2010-05-04 16:19:12

Unix命令

2010-03-02 09:39:11

保護WCF服務
點贊
收藏

51CTO技術棧公眾號