WCF配置文件注意事项

WCF配置文件分为服务端配置文件及客户端配置文件,很多配置项都是需要前后台同时支持的,具体的信息可以参见:WCF配置文件注释 及 WCF配置文件全攻略

1.maxStringContentLength="2097152"

在客户端配置 maxStringContentLength

2. The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

此异常信息非常明确的指出应当修改客户端配置文件中的 MaxReceivedMessageSize属性,可参考如下代码片段:

maxReceivedMessageSize配置代码片段
       < basicHttpBinding >
        
< binding  name ="BasicHttpBinding_IPermissionService"  closeTimeout ="00:01:00"
            openTimeout
="00:01:00"  receiveTimeout ="00:10:00"  sendTimeout ="00:01:00"
            allowCookies
="false"  bypassProxyOnLocal ="false"  hostNameComparisonMode ="StrongWildcard"
            maxBufferSize
="655360000"  maxBufferPoolSize ="524288"  maxReceivedMessageSize ="655360000"
            messageEncoding
="Text"  textEncoding ="utf-8"  transferMode ="Buffered"
            useDefaultWebProxy
="true" >

需要注意的是,好像需要同时修改maxBufferSize属性的值与MaxReceivedMessageSize属性值相同,否则还会报出另外的异常。

 

3. The maximum read depth (32) has been exceeded because XML data being read has more levels of nesting than is allowed by the quota. This quota may be increased by changing the MaxDepth property on the XmlDictionaryReaderQuotas object used when creating the XML reader.

此异常信息指示应当修改 MaxDepth属性的值,但修改客户端配置是没有用的,应当参考如下代码修改服务器端配置:

关于MaxDepth的配置代码片段
   < system.serviceModel >
    
< bindings >
      
< basicHttpBinding >
        
< binding  name ="MyBinding"  maxReceivedMessageSize ="6553600" >
          
< readerQuotas  maxDepth ="2147483647"  maxStringContentLength ="6553600"   />
        
</ binding >
     
</ basicHttpBinding >
    
</ bindings >
    
< services >
      
< service  name ="CohlFw3.Permission.Service.PermissionService"  behaviorConfiguration ="MyBehavior" >
        
< endpoint  address =""  binding ="basicHttpBinding"  bindingConfiguration ="MyBinding"  contract ="CohlFw3.Permission.Model.IPermissionService" />
      
</ service >       
    
</ services >
  
</ system.serviceModel >
  
<!-- 以上bindings和services节点默认是没有的,需要手工添加 -->


4. There was an error while trying to serialize parameter http://tempuri.org/:GetSuppliersResult. The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '.  Please see InnerException for more details.

在获取客商档案时,由于数据量较大,首先报出了如下错误:

http://localhost/cdms3.Service/BasicData/BasicDataService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

为了获取更详细的错误信息,只得祭起svcTraceViewer大旗,关于svcTraceViewer及svcConfigEditor工具的使用可参见:使用svcTraceViewer来调试WCF异常

于是便找到了如上面标题所述的异常信息,由此可知应当修改MaxItemsInObjectGraph属性值,在上述引用的文章中其实已经给出了解决方案,只是未给出实例代码,像我这种初学者还是花了点时间来配置MaxItemsInObjectGraph属性,应当同时修改前后台配置文件。

1)修改服务器端配置文件,在当前使用的Behavior节点中增加<dataContractSerializer maxItemsInObjectGraph="6553600" />,示例代码片段如下:

设置maxItemsInObjectGraph属性
         < behavior >
          
<!--  To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment  -->
          
< serviceMetadata  httpGetEnabled ="true" />
          
<!--  To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information  -->
          
< serviceDebug  includeExceptionDetailInFaults ="false" />
          
< dataContractSerializer  maxItemsInObjectGraph ="6553600"   />
        
</ behavior >

2)然后修改客户端配置文件,增加maxItemsInObjectGraph配置

客户端增加maxItemsInObjectGraph配置
     < behaviors >
      
< endpointBehaviors >
        
< behavior >
          
< dataContractSerializer  maxItemsInObjectGraph ="6553600"   />
        
</ behavior >
      
</ endpointBehaviors >
    
</ behaviors >

  如果在上述配置节点中为Behavior节点设置了名称,如:<behavior name="MyBehavior">,那么还应当修改endpoint节点,设定其使用的Behavior配置:behaviorConfiguration="MyBehavior",凡是未特别指定behaviorConfiguration的endpoint将使用默认的Behavior配置(即无name属性的behavior)。

 

你可能感兴趣的:(配置文件)