zl程序教程

您现在的位置是:首页 >  .Net

当前栏目

WCF初次操作实践

2023-03-15 21:58:23 时间

  我们通过实现一个简单的示例来对WCF有个直观而浅显的认识,希望对初次涉及WCF的朋友有所帮助。

  可以简单地认为WCF程序分为4部分:契约、服务、宿主、客户端。我们通过一个例子来逐步完成各部分,示例程序中,客户端可以获取一个信息列表,列表中每一项包括ID、值、读值时刻、状态、状态变动时刻。这里我用的是VS2010。

  首先,创建一个空白解决方案WCFDemo。

  我们将在其中添加n个项目,分别实现契约、服务、宿主、客户端。如果用VS2010新建“WCF服务库”或者“WCF服务应用程序”,它会默认把契约和服务放在一个项目中,我们这个示例把契约和服务分别放在2个类库项目中。

  第一步:契约

  1、添加一个类库WCFDemo.Contracts。

  2、在类库中添加2个文件DataContracts.cs和ServiceContracts.cs,分别放置数据契约和服务契约。

  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用来放置实现服务的类。

  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中针对每个属性定义私有字段,并提供带参数的构造函数,构造函数中对字段赋值而不是对属性赋值,那么每个DemoData实例化时比这里的示例代码效率高。)

  到这里,服务和契约已经完成。

  剩下的就是宿主如何对外提供服务和客户端如何享受服务了,我们先使用最最简单的方式来实现。

  我们先以最简单的方式来实现宿主和客户端:直接引用契约和服务项目、采用硬编码的方式。

  第三步:宿主

  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性能指标和调优
  4. 手把手教你jQuery jqPlot画图插件
  5. 从零开始学习jQuery之万能的选择器