WCF 的优势和特点

一、理解面向服务(Service-Oriented-Architecture)

   是指为了解决在Internet环境下业务集成的需要,通过连接能完成特定任务的独立功能实体实现的一种软件系统架构。SOA是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来。
SOA指出当前系统应该足够灵活,从而允许在不打乱当前成功运行的体系结构和基础结构的前提下,改动已有的体系结构。
 
SOA原则:
 
边界清晰
服务自治
兼容性基于策略

共享模式(schma)和契约

 

二、什么是WCF

  WCF(Windows Communication Foundation)是用于构建面向服务的应用程序的框架 ,是由微软发展的一组数据通信的应用程序开发接口。
 根据MSDN上的定义:WCF为.NetFramework提供了一个基础,使其能够编写代码,以在组件、应用程序、系统之间进行通信。WCF的设计遵循的是面向服务的原则。服务是指可以通过消息与之进行交互的一段代码。服务是被动的。它们等待传入消息之后才开始工作。客户端是发起者,客户端将消息发送给服务来请求工作。
 
 假定我们要为一家汽车租赁公司开发一个新的应用程序,用于租车预约服务。该租车预约服务会被多咱应用程序访问,包括:呼叫中心(Call Center),基于J2EE的租车预约服务以及合作伙伴的应用程序(Partner Application)
 
 
 
 使用WCF,该解决方案的实现就容易多了。如图中所示,WCF可用于前述所有情况。因此,租车预定应用程序使用这一种技术就可以实现其所有应用程序间的通信。
 
 WCF可使用Web服务进行通信,因此与同样支持SOAP的其他平台(例如基于J2EE的主流应用程序服务器)间的互操作性就变得简单明了。
 
 还可以对WCF进行配置和扩展,以便与使用并非基于SOAP的消息的Web服务进行通信。
 
 性能是大多数业务中至关重要的考虑事项。开发WCF的目标就是要使之成为Microsoft所开发的速度最快的分布式应用程序平台之一。
 
 
 
   WCF是提供统一的,可用于建立安全、可靠的面向服务的应用的高效开发平台。
   WCF具有如下的优势:  

  1、统一性

   2、互操作性

   3、安全与可信赖

   4、兼容性
 
 
 
 
 实例:
 
新建项目 选择wcf应用程序
 
[csharp] view plain copy
 
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace WcfService1  
  9. {  
  10.     // 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。  
  11.     [ServiceContract]  
  12.     public interface IService1  
  13.     {  
  14.   
  15.         [OperationContract]  
  16.         string GetData(int value);  
  17.   
  18.         [OperationContract]  
  19.         CompositeType GetDataUsingDataContract(CompositeType composite);  
  20.   
  21.         // 任务: 在此处添加服务操作  
  22.         [OperationContract]  
  23.         string HelloWorld();  
  24.            
  25.     }  
  26.   
  27.   
  28.     // 使用下面示例中说明的数据约定将复合类型添加到服务操作。  
  29.     [DataContract]  
  30.     public class CompositeType  
  31.     {  
  32.         bool boolValue = true;  
  33.         string stringValue = "Hello ";  
  34.   
  35.         [DataMember]  
  36.         public bool BoolValue  
  37.         {  
  38.             get { return boolValue; }  
  39.             set { boolValue = value; }  
  40.         }  
  41.   
  42.         [DataMember]  
  43.         public string StringValue  
  44.         {  
  45.             get { return stringValue; }  
  46.             set { stringValue = value; }  
  47.         }  
  48.     }  
  49. }  
 
实现接口:、
 
[csharp] view plain copy
 
 print?
  1. 在winform中调用这个返回字符串  
[csharp] view plain copy
 
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace WcfService1  
  9. {  
  10.     // 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。  
  11.     public class Service1 : IService1  
  12.     {  
  13.          
  14.  
  15.      
  16.  
  17.         #region IService1 成员  
  18.   
  19.   
  20.         string IService1.HelloWorld()  
  21.         {  
  22.             return "Hello WCF!";  
  23.         }  
  24.  
  25.         #endregion  
  26.  
  27.         #region IService1 成员  
  28.   
  29.         public string GetData(int value)  
  30.         {  
  31.             throw new NotImplementedException();  
  32.         }  
  33.   
  34.         public CompositeType GetDataUsingDataContract(CompositeType composite)  
  35.         {  
  36.             throw new NotImplementedException();  
  37.         }  
  38.  
  39.         #endregion  
  40.     }  
  41. }  
 
新建winform项目添加服务
 

 

[csharp] view plain copy
 
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsFormsApplication1  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.   
  22.             ServiceReference1.Service1Client client = new WindowsFormsApplication1.ServiceReference1.Service1Client();  
  23.             MessageBox.Show(client.HelloWorld());  
  24.           
  25.         }  
  26.     }  
  27. }  


 

 
 
 
第一天对WCF的认识就到这里。明天继续学习 。。。。。。。。。。。。。。。
 
 
 *作者:Stephenzhou(阿蒙)      
 *日期: 2012.08.02     
 *Mail:szstephenzhou@163.com      
 *另外:转载请著名出处。 
 *博客地址:http://blog.csdn.net/szstephenzhou
posted @ 2017-03-13 00:09  Mr▪King  阅读(455)  评论(0编辑  收藏  举报