WCF 的优势和特点

来源:互联网 发布:node代理服务器 跨域 编辑:程序博客网 时间:2024/05/17 22:58

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

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

共享模式(schma)和契约

 

二、什么是WCF

  WCFWindows 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应用程序
 
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace WcfService1{    // 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。    [ServiceContract]    public interface IService1    {        [OperationContract]        string GetData(int value);        [OperationContract]        CompositeType GetDataUsingDataContract(CompositeType composite);        // 任务: 在此处添加服务操作        [OperationContract]        string HelloWorld();             }    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。    [DataContract]    public class CompositeType    {        bool boolValue = true;        string stringValue = "Hello ";        [DataMember]        public bool BoolValue        {            get { return boolValue; }            set { boolValue = value; }        }        [DataMember]        public string StringValue        {            get { return stringValue; }            set { stringValue = value; }        }    }}

 
实现接口:、
 
 在winform中调用这个返回字符串
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace WcfService1{    // 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。    public class Service1 : IService1    {                    #region IService1 成员        string IService1.HelloWorld()        {            return "Hello WCF!";        }        #endregion        #region IService1 成员        public string GetData(int value)        {            throw new NotImplementedException();        }        public CompositeType GetDataUsingDataContract(CompositeType composite)        {            throw new NotImplementedException();        }        #endregion    }}

 
新建winform项目添加服务
 

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            ServiceReference1.Service1Client client = new WindowsFormsApplication1.ServiceReference1.Service1Client();            MessageBox.Show(client.HelloWorld());                }    }}


 

 
 
 
第一天对WCF的认识就到这里。明天继续学习 。。。。。。。。。。。。。。。
 
 
 *作者:Stephenzhou(阿蒙)     
 *日期: 2012.08.02    
 *Mail:szstephenzhou@163.com     
 *另外:转载请著名出处。
 *博客地址:http://blog.csdn.net/szstephenzhou