wcf 常见问题解决

1. 序列化最大值问题,如下:

在一些接口需要序列化的数据很大,就会报错,而数据量小则没有问题,

InnerException 消息是“对象图中可以序列化或反序列化的项目数目上限为“65536”。请更改对象图或增加 MaxItemsInObjectGraph 的配额

这个时候需要修改 MaxItemsInObjectGraph

MaxItemsInObjectGraph的解释如下:

拒绝服务(DoS- Denial of Service)是一种常用的黑客攻击行为,黑客通过生成大容量的数据频繁地对服务器发送请求,最终导致服务器不堪重负而崩溃。对于WCF的序列化或反序列化来说,数据的容量越大、成员越多、层次越深,序列化的时间就越长,耗用的资源就越多,如果黑客频繁地发送一个海量的数组过来,那么服务就会因为忙于进行反序列化的工作而没有足够的资源处理正常的请求,从而导致瘫痪。

在这种情况下,我们可以通过MaxItemsInObjectGraph这个属性设置DataContractSerializer允许被序列化或者反序列化对象数量的上限。一旦超过这个设定上限,序列化或者反序列化的工作将会立即中止,从而在一定程度上解决了通过发送大集合数据形式的拒绝服务攻击。DataContractSerializer中定义了以下3个重载的构造函数使我们能够设定MaxItemsInObjectGraph属性。

怎么做,如下:

解决方法:
用SvcConfigEditor为客户端和服务端都加上一个behavior (服务端添加servicehehavior, 客户端添加endpointbehavior),然后为这两个服务各添加一个dataContractSerializer,把他的maxItemsInObjectGraph字段变成更大的数。
这里是我这里服务端添加的log:
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8000/ContactManager/md" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="65536000" /> </behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="WcfServiceLibrary1.ContactManager">
<endpoint address="http://localhost:8000/ContactManager" binding="wsHttpBinding"
bindingConfiguration="NewBindingStationCnfg" contract="WcfServiceLibrary1.IContactManager" />
<endpoint address="http://localhost:8000/ContactManager/mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="NewBindingStationCnfg" maxReceivedMessageSize="483647" />
</wsHttpBinding>

</bindings>
</system.serviceModel>


这里是我客户端的config:

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="NewBehavior">
<dataContractSerializer maxItemsInObjectGraph="6553600" />
</behavior>
</endpointBehaviors> </behaviors>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IContactManager1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/ContactManager" behaviorConfiguration="NewBehavior"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IContactManager1"
contract="ServiceReference2.IContactManager" name="WSHttpBinding_IContactManager1">
<identity>
<userPrincipalName value="vblab1@redmond.corp.microsoft.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>

也可以通过代码解决:

var option = from o in Endpoint.Contract.Operations
where o.Name == "importtoexcel"
select o.Behaviors.OfType<DataContractSerializerOperationBehavior>().First<DataContractSerializerOperationBehavior>();


foreach (var o in option )
o.MaxItemsInObjectGraph = 65534000;

2.
System.ServiceModel.FaultException

  Message="The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Test'. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XMLDictionaryReaderQuotas object used when creating the XML reader."

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors />
      <bindings>
        <netTcpBinding>
          <binding name="NewBinding0" maxReceivedMessageSize="6553600">
            <readerQuotas maxStringContentLength="6553600" />
          </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="Learn.Library.WCF.Service">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="NewBinding0"
          contract="Learn.Library.WCF.IService" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8080/service" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

这两个问题最常见了,呵呵


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值