WCF基本资料

WCF资料

1.Service Class:一个标记[ServiceContract](服务契约)Attribute(属性)的类,在其中可能包含多个方法。

除了标记一些WCF特有的Attribute(属性)外,这个类与一般类没有什么区别。

2.Host(宿主):可以是应用程序,如进程Windows Service等,它是WCFService运行的环境。

3.Endpoints(终结点):可以是一个,也可以是一组,它是WCF实现通信的核心要素。

WCFService由一个Endpoints集合组成,每一个Endpoint就是用于通信的入口,客户端和服务端通过Endpoint交换信息,如下图:

WCF基本资料_第1张图片

从图中我们可以看到一个Endpoint由三部分组成:Address,Binding,Contract。便于记忆,

我们往往将这三部分称为是EndpointABC

WCF的三大核心是ABC也就是

A代表Address-where(对象在哪里)
B代表Binding-how(通过什么协议取得对象)
C代表Contract(契约)-what(定义的对象是什么,如何操纵)

Address:是Endpoint的网络地址,它标记了消息发送的地址。

Binding:描述的是如何发送消息,例如消息发送的传输协议(如TCPHTTP),安全(如SSLSOAP消息安全)。

Contract:则描述的是消息所包含的内容,以及消息的规范和操作方式。

所以Endpoint中的ABC分别代表的含义就是:wherehowwhat。当WCF发送消息时,通过address知道信息发送的地址,通过binding知道信息怎么样来发送的,通过contract则知道发送的消息时什么。

SOAP:简单对象访问协议,简单对象访问协议(SOAP)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。 SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议( HTTP),简单邮件传输协议SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序

 

合约(Contract)

WCF 的基本概念是以合约(Contract) 来定义双方沟通的协议,合约必须要以接口的方式来体现,而实际的服务代码必须要由这些合约接口派生并实现。合约分成了四种:数据合约 (Data Contract),订定双方沟通时的数据格式。服务合约 (Service Contract),订定服务的定义。操作合约 (Operation Contract),订定服务提供的方法。消息合约 (Message Contract),订定在通信期间改写消息内容的规范。一个 WCF 中的合约,就如同下列代码所示:   using System; 

using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] // 服务合约    public interface ICalculator 

[OperationContract] // 操作合约   

double Add(double n1, double n2); 

[OperationContract] // 操作合约   

double Subtract(double n1, double n2); 

[OperationContract] // 操作合约   

double Multiply(double n1, double n2); 

[OperationContract] // 操作合约   

double Divide(double n1, double n2); 


 

协议绑定 (Binding)

由于 WCF 支持了 HTTPTCPNamed PipeMSMQPeer-To-Peer TCP 等协议,

HTTP又分为基本 HTTP 支持 (BasicHttpBinding) 以及 WS-HTTP 支持 (WsHttpBinding),而 TCP 亦支持 NetTcpBindingNetPeerTcpBinding 等通信方式,因此,双方必须要统一通信的协议,并且也要在编码以及格式上要有所一致。   

一个设置通信协议绑定的示例如下:   

<?xml version="1.0" encoding="utf-8" ?>

<configuration> 

<system.serviceModel> 

  <!-- 设定服务系结的资讯 --> 

  <services> 

<service name=" CalculatorService" > 

<endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="ICalculator" /> 

  </service> 

</services> 

 <!-- 设定通讯协定系结的资讯 --> 

 <bindings> 

<wsHttpBinding> 

   <binding name="Binding1"> 

  </binding> 

  </wsHttpBinding> 

 </bindings> 

</system.serviceModel> 

</configuration> 
虽然 WCF 也可以使用 SOAP做通信格式,但它和以往的 ASP.NETXML Web Services不同,因此有部分技术文章中,会将 ASP.NET 的 XML Web Services 称为 ASMX ServiceWCF 的服务可以挂载于 Console ApplicationWindows ApplicationIIS (ASP.NET) ApplicationWindows Service以及 Windows Activation Services中,但大多都会挂在 Windows Service。 

 

安全性层次

WCF 实现上已经支持了传输层次安全性 (Transport-level security) 以及消息层次安全性 (Message-level security) 两种。传输层次安全性:在数据传输时期加密,例如 SSL。消息层次安全性:在数据处理时就加密,例如使用数字签名,散列或是使用密钥加密法等。

WCF所使用的凭据类型和说明

凭据

说明

指定客户端不需要提供任何凭据。这样可以转化为匿名客户端。

基本

为客户端指定基本身份验证。

摘要式

为客户端指定摘要式身份验证。

Ntlm

指定NT LAN Manager身份验证。在由于某种原因无法使用Kerberos身份验证时使用。还可以通过将AllowNtlm属性设置为false来禁止其用作退回。

Windows

指定Windows身份验证。若要在Windows域上仅指定Kerberos协议,则将AllowNtlm属性设置为false(默认值为true)

证书

使用X.509证书执行客户端身份验证

用户米/密码

用户必须提供用户名和密码。使用Windows身份验证或者其他自定义解决方案验证用户名/密码。

 

WCF支持的转传输安全模式

WCF中,一同支持5种传输安全模式,这些模式都是可配置的,如下所示列出了这些模式和其简要说明。

1.NoneNone模式指的是不采用任何传输安全机制。客户端不向服务端提供任何凭证,也不确保通道是安全的。None模式没有任何安全可言,通常是在绝对安全的网络孤岛内使用None模式,以提高系统效率。

2.Transport:当使用Transport传输/运送/运输)安全模式时,WCF将依靠传输通道来实现完整性,机密性和互相验证性的功能。可以提供安全通道传输协议包括HTTPTCPIPCMSMQ。也就是说,使用这些传输协议的绑定都可以使用Transport安全模式。

3.MessageMessage安全模式对消息本身进行加密来保证完整性,机密性和互相验证性。和Transport安全模式不同,Message安全模式不依赖任何传输通道或者网络,所以该模式的不需要考虑客户端和服务端之间是否直接通信,该安全模式是标准模式。

4.MixedMixed模式结合了Transport安全模式和Message安全模式,Mixed安全模式在实现消息完整性,机密性和服务端身份验证是采用了Transport安全模式,而在保护客户端安全凭证的安全时又采用了Message安全模式。

5.BothBoth安全模式同时使用了Transport安全模式和Message安全模式。在实际情况中很少使用Both模式,因为其过渡的安全机制使用导致了系统性能的下降。


标准绑定支持的安全模式及默认值

绑定名称

None安全模式

Transport安全模式

Message安全模式

Mixed模式

Both模式

basicHttpBinding

Yes(默认值)

Yes

Yes

Yes

No

webHttpBinding

Yes(默认值)

Yes

No

No

No

msmqIntegrationBinding

Yes

Yes(默认值)

No

No

No

netTcpBinding

Yes

Yes(默认值)

Yes

Yes

No

netMsmqBinding

Yes

Yes(默认值)

Yes

No

Yes

netNamedPipeBinding

Yes

Yes(默认值)

No

No

No

netPeerTcpBinding

Yes

Yes(默认值)

Yes

Yes

No

wsHttpBinding

Yes

Yes

Yes(默认值)

Yes

No

wsDualHtppBinding

Yes

No

Yes(默认值)

No

No

wsFederationHttpBinding

Yes

No

Yes(默认值)

Yes

No

ws20007HttpBinding

Yes

Yes

Yes(默认值)

Yes

No

ws2007FederationHttpBinding

Yes

No

Yes(默认值)

Yes

No

 

Transport安全模式下绑定支持的凭证

绑定名称

None

Windows

用户名/密码

证书

basicHttpBinding

Yes

Yes

Yes

Yes

webHttpBinding

Yes

Yes

Yes

Yes

msmqIntegrationHttpBinding

Yes

Yes

No

Yes

netTcpHttpBinding

Yes

Yes

No

Yes

netMsmqBinding

Yes

Yes

No

Yes

netNamedPipeBinding

No

Yes

No

No

netPeerTcpBinding

No

No

Yes

Yes

wsHttpBinding

Yes

Yes

Yes

Yes

wsDualHttpBinding

N/A

N/A

N/A

N/A

wsFederationHttpBinding

N/A

N/A

N/A

N/A

ws2007HttpBinding

Yes

Yes

Yes

Yes

ws2007FederationHttpBinding

N/A

N/A

N/A

N/A

 

Message安全模式下绑定支持的凭证

绑定名称

None

Windows

用户名/密码

证书

Issued Token

basicHttpBinding

No

No

No

Yes

No

webHttpBinding

N/A

N/A

N/A

N/A

N/A

msmqIntegrationHttpBinding

N/A

N/A

N/A

N/A

N/A

netTcpHttpBinding

Yes

Yes

Yes

Yes

Yes

netMsmqBinding

Yes

Yes

Yes

Yes

Yes

netNamedPipeBinding

N/A

N/A

N/A

N/A

N/A

netPeerTcpBinding

N/A

N/A

N/A

N/A

N/A

wsHttpBinding

Yes

Yes

Yes

Yes

Yes

wsDualHttpBinding

Yes

Yes

Yes

Yes

Yes

wsFederationHttpBinding

N/A

N/A

N/A

N/A

N/A

ws2007HttpBinding

Yes

Yes

Yes

Yes

Yes

ws2007FederationHttpBinding

N/A

N/A

N/A

N/A

N/A

你可能感兴趣的:(WCF基本资料)