什么是WCF
是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。
整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
是Windows平台上开发分布式应用最佳的实践方式。
WCF是.Net框架中的技术,用来创建面向服务的应用程序,交换不同通信方案里的消息,以及执行服务操作生成的工作流。
WCF应用程序由三部分组成 - WCF服务,WCF服务主机和WCF服务客户端。
WCF平台有时也被称为服务模型。 WCF的基本特征是互操作性。
这是微软用于构建面向服务的应用程序的最新技术之一。
根据基于消息的通信的概念中,一个HTTP请求可以被均匀地表示,
WCF是一个统一的API而不管不同的传输机制。
协议:WCF支持多种协议,即HTTP,命名管道,TCP和MSMQ;而Web服务仅支持HTTP协议。
托管机制:WCF托管不同的激活机制,即IIS(Internet信息服务),WAS(Windows激活服务),自托管和Windows服务,而Web服务则只能由IIS托管。
服务:WCF支持一个强大的安全,值得信赖的消息传递,事务性和互操作性,而Web服务只支持保障服务。
序列化:WCF支持DataContract串行采用System.Runtime.Serialization,而Web服务通过使用System.Xml.Serialization支持XML序列化。
工具:ServiceMetadata工具(svcutil.exe)用于客户机生成的WCF服务而WSDL.EXE工具用来产生相同web服务。
异常处理:在WCF中,未处理的异常都是在一个更好的方式通过使用FaultContract处理,并没有得到Web服务SOAP(简单对象访问协议)故障返回给客户端等。
有可能要序列哈希Tablein WCF,但这不能在web服务中。
绑定:WCF支持多种类型,如 basicHttpBinding,WSDualHttpBinding,WSHttpBinding等绑定,而Web服务仅支持SOAP或XML。
多线程:WCF支持多线程利用ServiceBehavIor类,而这Web服务不支持。
双工服务操作:WCF支持双工服务业务除了支持单向和请求 - 响应服务操作,而Web服务不支持双工服务操作。
地址:定义服务的地址
绑定:定义服务的通讯方式(传输协议,编码方法)
契约:定义服务的具体实现
终结点:有地址,绑定和契约共同构成的一个终结点
服务器通过终结点向客户端公开服务客户端通过终结点调用服务
构建WCF服务
namespace WCFDemo
{
[ServiceContract]
public interface Iservice
{
[OperationBehavior]
int Add(int a,int b);
}
public class Service : Iservice
{
public int Add(int a, int b)
{
throw new NotImplementedException();
}
}
}
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services><!--(服务名(实现方法名))-->
<service name="WCFDemo.Service" behaviorConfiguration="behaviorConfiguration">
<host>
<baseAddresses><!--(发布地址)-->
<add baseAddress="地址"/>
</baseAddresses>
</host><!--(服务契约)-->
<endpoint address="" binding="basicHttpBinding" contract="WCFDemo.Iservice"></endpoint>
</service>
<!--<service name="WCFDemo.Service" behaviorConfiguration="behaviorConfiguration">
<host>(——多个服务继续加service——)
<baseAddresses>
<add baseAddress="地址"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFDemo.Iservice"></endpoint>
</service>-->
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
启动服务
using System;
using System.ServiceModel;
namespace WCFDemo
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost service = new ServiceHost(typeof(Service)))
{
service.Open();
Console.ReadKey();
}
}
}
}
测试服务
vs安装目录下有一个wcftestclient.exe的小程序专用于测试wcf服务
是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。
整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
是Windows平台上开发分布式应用最佳的实践方式。
WCF是.Net框架中的技术,用来创建面向服务的应用程序,交换不同通信方案里的消息,以及执行服务操作生成的工作流。
WCF应用程序由三部分组成 - WCF服务,WCF服务主机和WCF服务客户端。
WCF平台有时也被称为服务模型。 WCF的基本特征是互操作性。
这是微软用于构建面向服务的应用程序的最新技术之一。
根据基于消息的通信的概念中,一个HTTP请求可以被均匀地表示,
WCF是一个统一的API而不管不同的传输机制。
协议:WCF支持多种协议,即HTTP,命名管道,TCP和MSMQ;而Web服务仅支持HTTP协议。
托管机制:WCF托管不同的激活机制,即IIS(Internet信息服务),WAS(Windows激活服务),自托管和Windows服务,而Web服务则只能由IIS托管。
服务:WCF支持一个强大的安全,值得信赖的消息传递,事务性和互操作性,而Web服务只支持保障服务。
序列化:WCF支持DataContract串行采用System.Runtime.Serialization,而Web服务通过使用System.Xml.Serialization支持XML序列化。
工具:ServiceMetadata工具(svcutil.exe)用于客户机生成的WCF服务而WSDL.EXE工具用来产生相同web服务。
异常处理:在WCF中,未处理的异常都是在一个更好的方式通过使用FaultContract处理,并没有得到Web服务SOAP(简单对象访问协议)故障返回给客户端等。
有可能要序列哈希Tablein WCF,但这不能在web服务中。
绑定:WCF支持多种类型,如 basicHttpBinding,WSDualHttpBinding,WSHttpBinding等绑定,而Web服务仅支持SOAP或XML。
多线程:WCF支持多线程利用ServiceBehavIor类,而这Web服务不支持。
双工服务操作:WCF支持双工服务业务除了支持单向和请求 - 响应服务操作,而Web服务不支持双工服务操作。
地址:定义服务的地址
绑定:定义服务的通讯方式(传输协议,编码方法)
契约:定义服务的具体实现
终结点:有地址,绑定和契约共同构成的一个终结点
服务器通过终结点向客户端公开服务客户端通过终结点调用服务
构建WCF服务
namespace WCFDemo
{
[ServiceContract]
public interface Iservice
{
[OperationBehavior]
int Add(int a,int b);
}
public class Service : Iservice
{
public int Add(int a, int b)
{
throw new NotImplementedException();
}
}
}
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services><!--(服务名(实现方法名))-->
<service name="WCFDemo.Service" behaviorConfiguration="behaviorConfiguration">
<host>
<baseAddresses><!--(发布地址)-->
<add baseAddress="地址"/>
</baseAddresses>
</host><!--(服务契约)-->
<endpoint address="" binding="basicHttpBinding" contract="WCFDemo.Iservice"></endpoint>
</service>
<!--<service name="WCFDemo.Service" behaviorConfiguration="behaviorConfiguration">
<host>(——多个服务继续加service——)
<baseAddresses>
<add baseAddress="地址"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFDemo.Iservice"></endpoint>
</service>-->
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
启动服务
using System;
using System.ServiceModel;
namespace WCFDemo
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost service = new ServiceHost(typeof(Service)))
{
service.Open();
Console.ReadKey();
}
}
}
}
测试服务
vs安装目录下有一个wcftestclient.exe的小程序专用于测试wcf服务