大家可能知道,WCF这样一款功能强大的开发插件,其中有很多内容值得我们去深入的探讨研究。在这篇文章中,我们将会针对其中一个比较重要的属性,WCF dataContractSerializer的相关设置进行一个详细的介绍。#t#
在WCF的使用过程中,有时会碰到传递的数据量比较巨大,客户端程序报错。常见的解决方法就是通过增大改变服务端与客户端配置文件serviceBehaviors的dataContractSerializer 的maxItemsInObjectGraph属性。有没有不通过配置文件,直接硬编码增大maxItemsInObjectGraph的值呢(此文不是考虑那种做法的优劣,而仅仅是探讨一种可能)?答案是肯定的。方法如下。
既然wcf的maxItemsInObjectGraph可以通过配置去设定,那么应该也有可能通过代码去设定。因为maxItemsInObjectGraph 通过配置文件信息的读取到这个信息后,最终应该把读到的信息去设置某个类的属性,从而改变服务的配置。
1. 先从有配置文件时,服务的运行开始着手。服务端配置文件如下
- < behaviors>
- < serviceBehaviors>
- < behavior name="Service1Behavior">
- < serviceMetadata httpGetEnabled="true" />
- < serviceDebug includeExceptionDetailInFaults="true" />
- < dataContractSerializer maxItemsInObjectGraph="2147483647" />
- < /behavior>
- < /serviceBehaviors>
- < /behaviors>
- < /system.serviceModel>
在ServiceHost实例化完成后加入如下的代码:
- public void Start()
- {
- ServiceHost host = new ServiceHost(typeof(Service1));
- Console.WriteLine("---------------------------------");
- foreach (var item in host.Description.Behaviors)
- {
- Console.WriteLine(item.ToString());
- }
- Console.WriteLine("---------------------------------");
- host.Open();
- }
在输出窗口得到以下信息:
---------------------------------
System.ServiceModel.ServiceBehaviorAttribute
Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior
System.ServiceModel.Description.ServiceMetadataBehavior
System.ServiceModel.Description.ServiceDebugBehavior
System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior
System.ServiceModel.Description.ServiceAuthorizationBehavior
进一步跟踪得知。WCF dataContractSerializer对应的类就是DataContractSerializerServiceBehavior。
2. 构造DataContractSerializerServiceBehavior实例,设置maxItemsInGraph属性,并把实例插入host.Description.Behaviors中。
用reflector 查看DataContractSerializerServiceBehavior,发现声明如下
internal class DataContractSerializerServiceBehavior£ºIServiceBehavior, IEndpointBehavior{}看来只能用反射类实例化它了。具体代码如下:
- ServiceHost host = new ServiceHost(typeof(Service1));
- Type t = host.GetType();
- object obj= t.Assembly.CreateInstance("System.ServiceModel.
Dispatcher.DataContractSerializerServiceBehavior",true,
BindingFlags.CreateInstance|BindingFlags.Instance|
BindingFlags.NonPublic ,null,new object[]{false ,Int32.MaxValue },
null,null);- IServiceBehavior myServiceBehavior=obj as IServiceBehavior ;
- if (myServiceBehavior != null)
- {
- host.Description.Behaviors.Add(myServiceBehavior);
- }
- Console.WriteLine("---------------------------------");
- foreach (var item in host.Description.Behaviors)
- {
- Console.WriteLine(item.ToString());
- }
- Console.WriteLine("---------------------------------");
- host.Open();3. 然后注释掉配置文件的dataContractSerializer
- < behaviors>
- < serviceBehaviors>
- < behavior name="Service1Behavior">
- < serviceMetadata httpGetEnabled="true" />
- < serviceDebug includeExceptionDetailInFaults="true" />
- < !--< dataContractSerializer maxItemsInObjectGraph="2147483647" />-->
- < /behavior>
- < /serviceBehaviors>
- < /behaviors>
4. 经测试,硬编码设置maxItemsInObjectGraph,与WCF dataContractSerializer设置配置文件的属性能达到同样的效果