WCF(Windows Communication Foundation)和WPF一样复杂, 这里仅记录一下WCF的基本知识.
面向服务架构
WCF是微软目前的分布式通信框架(.Net3.0和WPF一起推出, 现是.Net4.5版本). 在接触WCF之前, 只知道Communication的双方, 一个叫Client, 一个叫Server. 在WCF中, Server换成了Service. 语义上来讲, 都和服务相关, 但Server和Service并不等同.
为什么叫Service, 这和面向服务架构(SOA, Service-Oriented Architecture)有关. WCF的Wiki是这样描述的:
WCF is a tool often used to implement and deploy a service-oriented architecture (SOA). It is designed using service-oriented architecture principles to support distributed computing whereservices have remote consumers. Clients can consume multiple services; services can be consumed by multiple clients. Services are loosely coupled to each other. Services typically have a WSDL interface (Web Services Description Language) that any WCF client can use to consume the service, regardless of which platform the service is hosted on. WCF implements many advanced Web services (WS) standards such as WS-Addressing, WS-ReliableMessaging and WS-Security.
顺便说一下, 目前兴起一种叫做微服务的架构方式, 是从SOA发展而来. SOA的提出是在企业应用, 而微服务架构是从互联网应用兴起的. 企业应用, 不要被企业两个字吓住, 如果不是交易系统的话,并发量都不是很大的. 而互联网应用的用户规模和并发量相比企业应用就大很多.
SOA中的服务(Services)就是公开的一组功能的集合. 从软件设计的角度考虑, 软件设计思想经历了从函数发展到对象,从对象发展到组件,再从组件发展到服务的几次变迁.
消费服务的一方叫做Client. Client与Service通过一种SOAP(简单对象访问模型)的消息进行通信, SOAP是一种XML格式. 和具体的传输协议无关.
这样Client和Service就分离开来了. WCF的Client可以与非WCF的Service交互. WCF的Service也可以与非WCF的Client交互.如此, 就做到分布式和松耦合.
Client与Service之间, 并不是原始的类似Socket那样收发消息,而是通过RPC的方式进行交互.既然是RPC的方式, 那么Client需要引用Service的相关功能. 怎么做到呢?
Service通过公开元数据(metadata)的方式公开服务.一个非WCF的Client可以将元数据导入到本地环境中, 生成本地的类型数据. 类似的, WCF的Client也可以导入非WCF的Service的元数据, 然后以本地CLR类与接口的方式进行调用.
具体来讲, 在WCF中, Client使用proxy将服务调用转发给Service. proxy是Service在本地环境中的代理, 接口和Service一样. proxy是通过Service公开的元数据生成的(借助相关工具).
WCF不仅允许Client和Service跨机器交互
也允许Client和Service在同一机器上交互
WCF架构
不仅限于简单的通信, WCF还提供了对可靠性、事务性、并发管理、安全性等技术的支持.它们依赖基于拦截(interception)机制的WCF体系架构.
这部分引用Programming WCF Service 4th P76
The interception starts when the proxy serializes the call stack frame to a
message and sends the message down a chain of channels. The channel is merely an
interceptor whose purpose is to perform a specific task. Each client-side channel does
pre-call processing of the message. The exact structure and composition of the chain
depend mostly on the binding. For example, one of the channels may be responsible
for encoding the message (binary, text, or MTOM), another for passing the security
call context, another for propagating the client transaction, another for managing the
reliable session, another for encrypting the message body (if so configured), and so
on. The last channel on the client side is the transport channel, which sends the mes‐
sage over the configured transport to the host.
具体有哪些Channel, 依赖于需求和配置.
On the host side, the message goes through another chain of channels that perform
host-side pre-call processing of the message. The first channel on the host side is the
transport channel, which receives the message from the transport. Subsequent chan‐
nels perform various tasks, such as decryption of the message body, decoding of the
message, joining the propagated transaction, setting the security principal, managing
the session, and activating the service instance. The last channel on the host side
passes the message to the dispatcher. The dispatcher converts the message to a stack
frame and calls the service instance.

The service has no way of knowing that it was not called by a local client. In fact, it
was called by a local client—the dispatcher.
The service instance executes the call and returns control to the dispatcher, which
then converts the returned values and error information (if any) into a return mes‐
sage. The process is then reversed: the dispatcher passes the message through the
host-side channels to perform post-call processing, such as managing the transaction,
deactivating the instance, encoding the reply, encrypting it, and so on. The returned
message then goes to the transport channel, which sends it to the client-side channels
for client-side post-call processing. This process in turn consists of tasks such as
decryption, decoding, committing or aborting the transaction, and so on. The last
channel passes the message to the proxy, which converts the returned message to a
stack frame and returns control to the client.
参考:
- Programming WCF Service 4th
- understanding-wcf-bindings-and-channel-stack(http://www.c-sharpcorner.com/UploadFile/81a718/understanding-wcf-bindings-and-channel-stack/)
- Introducing Windows Communication Foundation in .NET Framework 4 (https://msdn.microsoft.com/en-us/library/ee958158.aspx)