【问题标题】:WCF Exception: The maximum message size quota for incoming messages (65536) has been exceededWCF 异常:已超出传入邮件 (65536) 的最大邮件大小配额
【发布时间】:2012-03-13 17:31:00
【问题描述】:

当我调用 WCF 服务时出现异常:

已超出传入邮件的最大邮件大小配额 (65536)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。

当我在 http 上使用 Wireshark 数据包分析器过滤器时,发送的最大数据包为 1226 字节,远低于 65536 字节的限制。关于为什么抛出此异常的任何建议?

协议长度信息

服务器堆栈跟踪:

at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
... 
    <binding 
        name="WSHttpBinding_IService" 
        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="65536"
        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="Certificate" 
          negotiateServiceCredential="true"
          algorithmSuite="Default" />
      </security>
    </binding>

我怀疑导致异常的代码:

public LoanPlan CalculateLoanPlans(string productName)
{
    var loanPlan = new LoanPlan
    {
        Details = new[]
        {
            new PlanDetails {LoanAmount = 5000, Periods = 6},
            new PlanDetails {LoanAmount = 5000, Periods = 12},
            new PlanDetails {LoanAmount = 5000, Periods = 24},

            new PlanDetails {LoanAmount = 10000, Periods = 6},
            new PlanDetails {LoanAmount = 10000, Periods = 12},
            new PlanDetails {LoanAmount = 10000, Periods = 24},

            new PlanDetails {LoanAmount = 15000, Periods = 6},
            new PlanDetails {LoanAmount = 15000, Periods = 12},
            new PlanDetails {LoanAmount = 15000, Periods = 24},

            new PlanDetails {LoanAmount = 20000, Periods = 6},
            new PlanDetails {LoanAmount = 20000, Periods = 12},
            new PlanDetails {LoanAmount = 20000, Periods = 24},

            new PlanDetails {LoanAmount = 30000, Periods = 6},
            new PlanDetails {LoanAmount = 30000, Periods = 12},
            new PlanDetails {LoanAmount = 30000, Periods = 24},

            new PlanDetails {LoanAmount = 40000, Periods = 6},
            new PlanDetails {LoanAmount = 40000, Periods = 12},
            new PlanDetails {LoanAmount = 40000, Periods = 24},

            new PlanDetails {LoanAmount = 50000, Periods = 6},
            new PlanDetails {LoanAmount = 50000, Periods = 12},
            new PlanDetails {LoanAmount = 50000, Periods = 24}
        },
        TaxProcent = _taxPercent,
     };
     Parallel.ForEach(loanPlan.Details, detail =>
     {
         var result = Calculate(productName, Convert.ToInt32(detail.LoanAmount), detail.Periods, null);

         detail.ActualPaymentCost = result.ActualPaymentCost;
         detail.CreditCost = result.CreditAmount;
         detail.MonthlyPayment = result.MonthlyPayment;
         detail.MonthlyPaymentCost = result.MonthlyPaymentCredit;
     });

     var firstDetail = loanPlan.Details[0];
     var firstResult = Calculate(productName, Convert.ToInt32(firstDetail.LoanAmount), firstDetail.Periods, null);

     loanPlan.MonthlyFee = firstResult.MonthlyFee;
     loanPlan.MonthlyInterest = firstResult.MonthlyInterest;

     return loanPlan;
}

Calculate 方法包含服务调用。

【问题讨论】:

  • 你能发布你的配置条目吗?
  • 另外,请在客户端和服务器的配置中的绑定元素中设置以下 readerQuotas: 和行为配置元素中的 datacontract 序列化程序: 并设置 maxRecievedMessageSize,如下所示:
  • 错误不是来自服务器回复而不是你的请求吗?
  • 您需要在客户端和服务器上进行相同的设置
  • 为什么大多数人在面对 WCF 中的此类长度问题时会本能地建议将所有与长度相关的属性限制为不小于其最大容量?

标签: c# asp.net wcf service


【解决方案1】:

增加服务器和客户端的 maxStringContentLength & maxArrayLength="2147483647"。检查下面的链接,详细说明

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f570823a-8581-45ba-8b0b-ab0c7d7fcae1

【讨论】:

  • 我认为问题不是如何解决,而是为什么会发生
  • 这是一个非常喜怒无常的修复,充其量。
  • 我已经在客户端完成了。在我让服务器端人员更改配置之前,我想确定这是问题所在。奇怪的是,当我使用wireshark时,我看不到任何大于发送限制的包。
  • 是的,您需要在服务器和客户端上都增加。
  • 问题是我怀疑真正的问题是消息大小。因为它工作了一小会儿,但是当我运行一个对服务进行特定调用的特定方法时,我得到了异常。以前有效的方法现在也抛出相同的异常。我在我的帖子中添加了一个代码 sn-p,它解释了我怀疑是什么调用导致它。
猜你喜欢
  • 2018-01-15
  • 2015-09-15
  • 2015-06-13
  • 2014-02-13
  • 2011-02-23
相关资源
最近更新 更多