本文介绍了在WCF中超过了最大请求长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用Wcf应用程序,并且在跟踪日志中遇到了上面提到的错误.

I am currently using Wcf application and getting above mentiooned error in Trace Log.

下面是Wcf服务的Web.Config.

Below is the Web.Config for Wcf Service.

<bindings>
          <wsHttpBinding>
              <binding name="NewBinding0" closeTimeout="00:50:00" openTimeout="00:50:00" sendTimeout="00:50:00" receiveTimeout="00:50:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
                  <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                  <reliableSession enabled="true" />
                  <security mode="None">
                      <transport clientCredentialType="None" />
                      <message clientCredentialType="Windows" negotiateServiceCredential="true" establishSecurityContext="false" />
                  </security>
              </binding>
          </wsHttpBinding>
      </bindings>

<diagnostics wmiProviderEnabled="true">
  <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<services>
  <service behaviorConfiguration="DBSyncWcfService.Service1Behavior" name="DBSyncWcfService.DBSyncService">
    <endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" name="ABC" contract="DBSyncWcfService.IDBSyncContract" address="http://192.168.5.170:9999/" />
    <host>
      <baseAddresses>

下面是客户端配置.

        WSHttpBinding binding = new WSHttpBinding();
        //binding.ReaderQuotas.MaxArrayLength = 10485760;

        //binding.MaxReceivedMessageSize = 10485760;
        binding.Security.Mode = SecurityMode.None;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
        binding.Security.Message.EstablishSecurityContext = false;
        //binding.Security.Message.NegotiateServiceCredential = true;
        binding.ReliableSession.Enabled = true;
        binding.ReaderQuotas.MaxArrayLength = 2147483647;
        binding.ReaderQuotas.MaxDepth = 2147483647;
        binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
        binding.ReaderQuotas.MaxBytesPerRead = 2147483647;
        //binding.MaxReceivedMessageSize = 20000000;2147483647
        binding.MaxReceivedMessageSize = 2147483647;
        //binding.MaxReceivedMessageSize = Int32.MaxValue;
        binding.ReaderQuotas.MaxStringContentLength = 2147483647;

        //binding.MaxBufferPoolSize = 20000000;
        binding.MaxBufferPoolSize = 2147483647;

        //binding.MaxBufferPoolSize = Int32.MaxValue;
        binding.ReaderQuotas.MaxArrayLength = 2147483647;
        binding.ReaderQuotas.MaxDepth = 2147483647;
        binding.SendTimeout = TimeSpan.FromMinutes(50);
        binding.CloseTimeout = TimeSpan.FromMinutes(50);
        binding.OpenTimeout = TimeSpan.FromMinutes(50);
        binding.ReceiveTimeout = TimeSpan.FromMinutes(50);

推荐答案

您可能必须设置绑定的readerQuotas,如下所示:

You might have to set the readerQuotas for the binding as shown below:

            WSHttpBinding binding1 = new WSHttpBinding();
            binding1.MaxBufferPoolSize = 2147483647;
            binding1.MaxReceivedMessageSize = 2147483647;

            var myReaderQuotas1 = new XmlDictionaryReaderQuotas();
            myReaderQuotas1.MaxStringContentLength = 2147483647;
            myReaderQuotas1.MaxDepth = 2147483647;
            myReaderQuotas1.MaxArrayLength = 2147483647;
            myReaderQuotas1.MaxBytesPerRead = 2147483647;
            myReaderQuotas1.MaxNameTableCharCount = 2147483647;
            binding1.GetType().GetProperty("ReaderQuotas").SetValue(binding1, myReaderQuotas1, null);

原因是在已经创建的绑定上设置配额无效.

The reason is that setting quotas on a binding that has already been created has no effect.

此外,您还需要考虑将客户端和服务器上的DataContractSerializer的"MaxItemsInObjectGraph"值增加到较大的值.

Also you would need to consider increasing your "MaxItemsInObjectGraph" value of the DataContractSerializer on both client and server to a large value.

这篇关于在WCF中超过了最大请求长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:31