---
title: "[原创]WCF技术剖析之一：通过一个ASP.NET程序模拟WCF基础架构"
author: "varsoft"
date: "2009-06-18T21:11:00Z"
tags: []
slug: "wcf-asp-net-wcf"
draft: false
lastmod: "2009-06-18T21:11:00Z"
id: 871311
url: "https://varsoft.iteye.com/blog/871311"
---

细算起来，已经有好几个月没有真正的写过文章了。近半年以来，一直忙于我的第一本WCF专著《WCF技术剖析》的写作，一直无暇管理自己的Blog。到目前为止《WCF技术剖析（卷1）》的写作暂告一段落，初步预计于下个月由武汉博文视点出版。在《WCF技术剖析》写作期间，对WCF又有了新的感悟，为此以书名开始本人的第三个WCF系列。本系列的目的在于对《WCF技术剖析》的补充，会对书中的一些内容进行展开讲述，同时会囊括很多由于篇幅的原因忍痛割弃的内容。

本系列的第一篇，我将会对WCF的基本架构作一个大致的讲解。不过，一改传统对WCF的工作流程进行平铺直叙，我将另辟蹊径，借助于我们熟悉的ASP.NET作为请求处理平台，通过一个简单的托管程序模拟整个WCF客户端和服务端的架构。Source Code下载：[Artech.WcfFrameworkSimulator.zip](http://files.cnblogs.com/artech/Artech.WcfFrameworkSimulator.zip)

**WCF框架处理流程和涉及的组件**

我们的模拟程序将你搭建一个迷你版的WCF框架，为了展示WCF整个处理流程中使用到一些特殊组件。我们首先来简单介绍一下对于一个简单的WCF服务调用，WCF的客户端和服务端框架的处理流程，和该流程的每一个阶段都使用那些重要组件。

下面的列表列出了WCF服务端框架对于处理一个简单的WCF服务调用请求所提供的功能，以及相应的功能承载的组件：

- **请求消息的接收和回复消息的发送：**服务端在传输层监听与接收来自客户的请求，并将经过编码后的回复消息通过传输层发送到客户端
- **请求消息的解码和回复消息的编码：**将接收到的字节数组通过解码生成请求消息对象，并将回复消息通过编程转化成字节组。消息的编码和解码通过MessageEncoder完成，而MessageEncoderFactory负责创建该对象
- **请求消息的反序列化和回复消息的序列化：**对请求消息进行反序列化，为服务操作的执行生成相应的输入参数，以及将服务操作执行的结果（返回值或者ref/out参数）序列化，并生成回复消息。序列化和反序列化通过DispatchMessageFormatter完成
- **服务对象的创建：**创建或者激活服务对象实例，InstanceProvider用于服务对象的创建或获取
- **服务操作的执行**：调用创建的服务对象的操作方法，并传入经过反序列化生成的输入参数。OperationInvoker完成对服务操作的最终执行

较之服务端的流程，客户端的流程显得相对简单，仅仅包含以下三个必需的阶段：

- **请求消息的序列化和回复消息的反序列化：**生成请求消息并将输入参数序列化到请求消息中，以及对回复消息进行反序列化，转化成方法调用的返回值或者ref/out参数。序列化和反序列化通过ClienthMessageFormatter完成
- **请求消息的编码和回复消息的解码：**对请求消息进行编码生成字节数组供传输层发送，以及将传输层接收到的字节数组解码生成恢复消息。消息的编码和解码通过MessageEncoder完成，而MessageEncoderFactory负责创建该对象
- **请求消息的发送和回复消息的接收**：在传输层将经过编码的请求消息发送到服务端，以及将接收来自服务端的恢复消息

[![clip_image002](http://images.cnblogs.com/cnblogs_com/artech/WindowsLiveWriter/WCFASP.NETWCF_12164/clip_image002_thumb.jpg "clip_image002")](http://images.cnblogs.com/cnblogs_com/artech/WindowsLiveWriter/WCFASP.NETWCF_12164/clip_image002_2.jpg)

**图1 精简版WCF客户端与服务端组件**

**图1**反映了进行服务调用的必要步骤和使用到的相关WCF组件。在本案例演示中，我们需要做的就是手工创建这些组件，并通过我们自己的代码利用它们搭建一个简易版的WCF框架。如果读者能够对本案例的实现有一个清晰的理解，相信对于整个WCF的框架就不会感到陌生了。

**图2**显示了本案例解决方案的基本结构，总共分三个项目。Contracts用于定义服务契约，被服务端和客户端引用。客户端通过一个Console应用模拟，而服务端则通过一个ASP.NET Website实现。

[![clip_image002\[5\]](http://images.cnblogs.com/cnblogs_com/artech/WindowsLiveWriter/WCFASP.NETWCF_12164/clip_image002%5B5%5D_thumb.jpg "clip_image002[5]")](http://images.cnblogs.com/cnblogs_com/artech/WindowsLiveWriter/WCFASP.NETWCF_12164/clip_image002%5B5%5D.jpg)

**图2 WCF框架模拟案例应用结构**

**步骤一、通过服务契约类型创建相关组件**

WCF在整个服务调用生命周期的不同阶段，会使用到不同的组件。我们通过一个方法将服务端和客户端所需的所有组件都创建出来，为此，我们在Contracts项目中添加了一个Utility类型，在Create&lt;T&gt;方法中创建所有的组件并通过输出参数的形式返回，泛型类型T表示的是服务契约类型。在该方法中，输出参数encoderFactory被服务端和客户端用于消息的编码和解码，clientFormatters和dispatchFormatters以字典的形式包含了基于服务操作的IClientMessageFormatter和IDispatchMessageFormatter，其中clientFormatters和dispatchFormatters的Key分别为操作名称和操作对应的Action。同样通过字典形式返回的operationInvokers和methods用于在服务端执行相应的操作方法，Key同样为操作对应的Action。

```
   1: public static class Utility
```

&lt;!--CRLF--&gt;

```
   2: {
```

&lt;!--CRLF--&gt;

```
   3:     public static void Create<T>(out MessageEncoderFactory encoderFactory,
```

&lt;!--CRLF--&gt;

```
   4:         out IDictionary<string, IClientMessageFormatter> clientFormatters,
```

&lt;!--CRLF--&gt;

```
   5:         out IDictionary<string, IDispatchMessageFormatter> dispatchFormatters,
```

&lt;!--CRLF--&gt;

```
   6:         out IDictionary<string, IOperationInvoker> operationInvokers,
```

&lt;!--CRLF--&gt;

```
   7:         out IDictionary<string, MethodInfo> methods)
```

&lt;!--CRLF--&gt;

```
   8:     {
```

&lt;!--CRLF--&gt;

```
   9:         //省略实现
```

&lt;!--CRLF--&gt;

```
  10:     }
```

&lt;!--CRLF--&gt;

```
  11: }
```

&lt;!--CRLF--&gt;

```
具体的实现如下，由于在WCF框架中使用的MessageEncoderFactory（TextMessageEncoderFactory）、MessageFormatter（DataContractSerializerOperationFormatter）和OperationInvoker（SyncMethodInvoker）都是一些内部类型，所以只能通过反射的方式创建它们。而操作名称和Action也主要通过反射的原理解析应用在服务方法上的OperationContractAttribute得到。
```

```
   1: public static void Create<T>(out MessageEncoderFactory encoderFactory,
```

&lt;!--CRLF--&gt;

```
   2:     out IDictionary<string, IClientMessageFormatter> clientFormatters,
```

&lt;!--CRLF--&gt;

```
   3:     out IDictionary<string, IDispatchMessageFormatter> dispatchFormatters,
```

&lt;!--CRLF--&gt;

```
   4:     out IDictionary<string, IOperationInvoker> operationInvokers,
```

&lt;!--CRLF--&gt;

```
   5:     out IDictionary<string, MethodInfo> methods)
```

&lt;!--CRLF--&gt;

```
   6: {
```

&lt;!--CRLF--&gt;

```
   7:     //确保类型T是应用了ServiceContractAttribute的服务契约
```

&lt;!--CRLF--&gt;

```
   8:     object[] attributes = typeof(T).GetCustomAttributes(typeof(ServiceContractAttribute), false);
```

&lt;!--CRLF--&gt;

```
   9:     if (attributes.Length == 0)
```

&lt;!--CRLF--&gt;

```
  10:     {
```

&lt;!--CRLF--&gt;

```
  11:         throw new InvalidOperationException(string.Format("The type \"{0}\" is not a ServiceContract!", typeof(T).AssemblyQualifiedName));
```

&lt;!--CRLF--&gt;

```
  12:     } 
```

&lt;!--CRLF--&gt;

```
  13: 
```

&lt;!--CRLF--&gt;

```
  14:     //创建字典保存IClientMessageFormatter、IDispatchMessageFormatter、IOperationInvoker和MethodInfo
```

&lt;!--CRLF--&gt;

```
  15:     clientFormatters = new Dictionary<string, IClientMessageFormatter>();
```

&lt;!--CRLF--&gt;

```
  16:     dispatchFormatters = new Dictionary<string, IDispatchMessageFormatter>();
```

&lt;!--CRLF--&gt;

```
  17:     operationInvokers = new Dictionary<string, IOperationInvoker>();
```

&lt;!--CRLF--&gt;

```
  18:     methods = new Dictionary<string, MethodInfo>(); 
```

&lt;!--CRLF--&gt;

```
  19: 
```

&lt;!--CRLF--&gt;

```
  20:     //MessageEncoderFactory
```

&lt;!--CRLF--&gt;

```
  21:     string encoderFactoryType = "System.ServiceModel.Channels.TextMessageEncoderFactory,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
```

&lt;!--CRLF--&gt;

```
  22:     encoderFactory = (MessageEncoderFactory)Activator.CreateInstance(Type.GetType(encoderFactoryType), MessageVersion.Default, Encoding.UTF8, int.MaxValue, int.MaxValue, new XmlDictionaryReaderQuotas()); 
```

&lt;!--CRLF--&gt;

```
  23: 
```

&lt;!--CRLF--&gt;

```
  24: //得到OperationDecription列表
```

&lt;!--CRLF--&gt;

```
  25: string defaultNamespace = "http://tempuri.org/";
```

&lt;!--CRLF--&gt;

```
  26:     ServiceContractAttribute serviceAttribute = (ServiceContractAttribute)attributes[0];
```

&lt;!--CRLF--&gt;

```
  27:     string serviceNamepace = string.IsNullOrEmpty(serviceAttribute.Namespace) ? defaultNamespace : serviceAttribute.Namespace;
```

&lt;!--CRLF--&gt;

```
  28:     string serviceName = string.IsNullOrEmpty(serviceAttribute.Name) ? typeof(T).Name : serviceAttribute.Name;
```

&lt;!--CRLF--&gt;

```
  29:     var operations = ContractDescription.GetContract(typeof(T)).Operations; 
```

&lt;!--CRLF--&gt;

```
  30: 
```

&lt;!--CRLF--&gt;

```
  31:     //得到具体的IClientMessageFormatter、IDispatchMessageFormatter和IOperationInvoker的具体类型
```

&lt;!--CRLF--&gt;

```
  32:     //IClientMessageFormatter+IDispatchMessageFormatter：DataContractSerializerOperationFormatter
```

&lt;!--CRLF--&gt;

```
  33:     //IOperationInvoker：SyncMethodInvoker
```

&lt;!--CRLF--&gt;

```
  34:     string formatterTypeName = "System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
```

&lt;!--CRLF--&gt;

```
  35:     Type formatterType = Type.GetType(formatterTypeName);
```

&lt;!--CRLF--&gt;

```
  36:     ConstructorInfo formatterConstructor = formatterType.GetConstructor(new Type[] { typeof(OperationDescription), typeof(DataContractFormatAttribute), typeof(DataContractSerializerOperationBehavior) });
```

&lt;!--CRLF--&gt;

```
  37:     string operationInvokerTypeName = "System.ServiceModel.Dispatcher.SyncMethodInvoker,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
```

&lt;!--CRLF--&gt;

```
  38:     Type operationInvokerType = Type.GetType(operationInvokerTypeName); 
```

&lt;!--CRLF--&gt;

```
  39: 
```

&lt;!--CRLF--&gt;

```
  40:     foreach (MethodInfo method in typeof(T).GetMethods())
```

&lt;!--CRLF--&gt;

```
  41:     {
```

&lt;!--CRLF--&gt;

```
  42:         attributes = method.GetCustomAttributes(typeof(OperationContractAttribute), true);
```

&lt;!--CRLF--&gt;

```
  43:         if (attributes.Length > 0)
```

&lt;!--CRLF--&gt;

```
  44:         {
```

&lt;!--CRLF--&gt;

```
  45:             OperationContractAttribute operationAttribute = (OperationContractAttribute)attributes[0];
```

&lt;!--CRLF--&gt;

```
  46:             string operationName = string.IsNullOrEmpty(operationAttribute.Name) ? method.Name : operationAttribute.Name;
```

&lt;!--CRLF--&gt;

```
  47:             //通过OperationContractAttribute得到Action
```

&lt;!--CRLF--&gt;

```
  48:             string action;
```

&lt;!--CRLF--&gt;

```
  49:             if (string.IsNullOrEmpty(operationAttribute.Action))
```

&lt;!--CRLF--&gt;

```
  50:             {
```

&lt;!--CRLF--&gt;

```
  51:                 action = string.Format("{0}{1}/{2}", serviceNamepace, serviceName, operationName);
```

&lt;!--CRLF--&gt;

```
  52:             }
```

&lt;!--CRLF--&gt;

```
  53:             else
```

&lt;!--CRLF--&gt;

```
  54:             {
```

&lt;!--CRLF--&gt;

```
  55:                 action = operationAttribute.Action;
```

&lt;!--CRLF--&gt;

```
  56:             } 
```

&lt;!--CRLF--&gt;

```
  57: 
```

&lt;!--CRLF--&gt;

```
  58:             OperationDescription operation = operations.Where(op => op.Name == operationName).ToArray<OperationDescription>()[0];
```

&lt;!--CRLF--&gt;

```
  59:             //通过反射创建DataContractSerializerOperationFormatter对象
```

&lt;!--CRLF--&gt;

```
  60:             object formatter = formatterConstructor.Invoke(new object[] { operation, new DataContractFormatAttribute(), null });
```

&lt;!--CRLF--&gt;

```
  61:             clientFormatters.Add(operationName, formatter as IClientMessageFormatter);
```

&lt;!--CRLF--&gt;

```
  62:             dispatchFormatters.Add(action, formatter as IDispatchMessageFormatter); 
```

&lt;!--CRLF--&gt;

```
  63: 
```

&lt;!--CRLF--&gt;

```
  64:             //通过反射创建SyncMethodInvoker对象
```

&lt;!--CRLF--&gt;

```
  65:             IOperationInvoker operationInvoker = (IOperationInvoker)Activator.CreateInstance(operationInvokerType, method);
```

&lt;!--CRLF--&gt;

```
  66:             operationInvokers.Add(action, operationInvoker);
```

&lt;!--CRLF--&gt;

```
  67:             methods.Add(action, method);
```

&lt;!--CRLF--&gt;

```
  68:         }
```

&lt;!--CRLF--&gt;

```
  69: }
```

&lt;!--CRLF--&gt;

[](http://11011.net/software/vspaste)

**步骤二、创建服务契约和实现服务**

接下来为本案例创建一个服务契约和实现该契约。服务契约定义在Contracts项目，具体的服务实现在模拟服务端的ASP.NET Web站点中。简单起见，依然沿用计算服务的例子。

```
   1: [ServiceContract(Namespace = "http://www.artech.com/")]
```

&lt;!--CRLF--&gt;

```
   2:     public interface ICalculator
```

&lt;!--CRLF--&gt;

```
   3:     {
```

&lt;!--CRLF--&gt;

```
   4:         [OperationContract]
```

&lt;!--CRLF--&gt;

```
   5:         double Add(double x, double y);
```

&lt;!--CRLF--&gt;

```
   6:     }
```

&lt;!--CRLF--&gt;

```
   1: public class CalculatorService : ICalculator
```

&lt;!--CRLF--&gt;

```
   2: {
```

&lt;!--CRLF--&gt;

```
   3:     public double Add(double x, double y)
```

&lt;!--CRLF--&gt;

```
   4:     {
```

&lt;!--CRLF--&gt;

```
   5:         return x + y;
```

&lt;!--CRLF--&gt;

```
   6:     }
```

&lt;!--CRLF--&gt;

```
   7: }
```

&lt;!--CRLF--&gt;

**步骤三、实现服务端对服务调用请求的处理**

我们通过一个ASP.NET的Web Page来模拟WCF服务端对服务请求的处理，下面的Calculator类型相关的代码实际上就是Calculator.aspx的后台代码（Code Behind）。整个处理流程不算复杂。在构造函数中，调用Utility的Create&lt;ICalculator&gt;方法，将所需的组件进行初始化，而具体的服务调用请求处理的逻辑在直接写在Web Page的Load事件中。

首先，通过MessageCoderFactory创建MessageEncoder对接收到的以HttpRequest形式体现的服务调用请求进行解码，并生成请求消息。通过请求消息得到当前服务操作的Action属性后，在初始化过程中得到的基于服务契约所有MethodInfo列表中，根据该Action得到当前操作对应的MethodInfo对象。借助于MethodInfo对象得到操作方法的输入参数和输出参数数量后，创建两个对象数组，分别用于保存通过DispatchMessageFormatter对象对于请求消息进行反序列化得到的输入参数，和通过OperationInvoker执行操作方法得到的输出参数。在OperationInvoker执行操作方法之前，通过反射的方式直接创建服务对象，这一步在真正的WCF框架中是通过InstanceProvider实现的。

通过OperationInvoker执行操作方法的结果有两种形式：返回值和输出参数（包括引用参数）。它们通过被传入DispatchMessageFormatter被序列