传入消息的最大消息大小配额为65536。要增加配额,请使用MaxReceivedMessageSize属性。

25

我遇到了一个奇怪的问题,正在尝试解决。我知道当我们处理大量数据时,必须增加客户端.config文件上的配额,但是如果我的客户端向WCF服务器发送巨大的数据,我应该怎么做?

当我发送小型输入参数时,它可以完美地运行。不幸的是,当输入变得更大时,它就会崩溃。

调试器显示:

Bad Request, 400;

跟踪文件中的信息为:

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

是否有办法在服务器端增加传入数据的配额?如果有,如何实现?

这是与配置文件相关的示例配置部分:

<bindings>
  <basicHttpBinding>
    <binding name="MyBasicHttpBinding"
        closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"
        sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false"
        hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647"
        maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
        useDefaultWebProxy="true">
      <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"  />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>

  </basicHttpBinding>
</bindings>

 <services>
  <service name="MyWcfService">
    <endpoint address="http://myservice..."
      binding="basicHttpBinding" bindingConfiguration="MyBasicHttpBinding"
      name="MyBasicHttpBinding" contract="IMyContract" />
  </service>
</services> 

这是我的客户端代码(我会动态创建它):

        var binding = new BasicHttpBinding();
        binding.MaxBufferPoolSize = 2147483647;
        binding.MaxBufferSize = 2147483647;
        binding.MaxReceivedMessageSize = 2147483647;
        binding.ReaderQuotas.MaxStringContentLength = 2147483647;
        binding.ReaderQuotas.MaxArrayLength = 2147483647;
        binding.ReaderQuotas.MaxDepth = 2147483647;
        binding.ReaderQuotas.MaxBytesPerRead = 2147483647;


        var address = new EndpointAddress("http://mylocalservice.."); 

        ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>(binding, address);

        foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
        {
            DataContractSerializerOperationBehavior dataContractBehavior =
                        op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                        as DataContractSerializerOperationBehavior;
            if (dataContractBehavior != null)
            {
                dataContractBehavior.MaxItemsInObjectGraph = 2147483646;
            }
        }
        public IMyContract MyClientObject = factory.CreateChannel();

1
我按照这里提到的步骤解决了这个问题。 - Ram
1
在客户端代码中,您正在通过代码设置读取器配额,因此以下是设置它们的方法: XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); myReaderQuotas.MaxStringContentLength = 2147483647; myReaderQuotas.MaxArrayLength = 2147483647; myReaderQuotas.MaxBytesPerRead = 2147483647; myReaderQuotas.MaxDepth = 64; myReaderQuotas.MaxNameTableCharCount = 2147483647;binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null); - Rajesh
这个回答解决了你的问题吗?传入消息的最大消息大小配额(65536)已超过 - KyleMit
3个回答

47
你可以通过服务的配置文件设置 MaxReceivedMessageSize 属性。设置客户端的 MaxReceivedMessageSize 只会影响客户端接收到的消息,对服务端接收到的消息没有影响。因此,要允许服务端接收大消息,你需要设置服务的配置文件。

示例服务配置

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="MyBinding" maxReceivedMessageSize="2147483647" />
    </wsHttpBinding>
  </bindings>
  <services>
    <service name="MyService">
      <endpoint address="http://myservice" binding="wsHttpBinding"
                bindingConfiguration="MyBinding" contract="IMyServiceContract" />
    </service>
  </services>
</system.serviceModel>

上面是一个非常简化的配置文件,但显示了相关部分。重要的部分是你定义绑定并给它一个名称,显式地设置任何与默认值不同的值,并在端点元素的bindingConfiguration属性中使用该名称。


问题是我在服务器端没有看到任何绑定配置。是否有一些默认的绑定或其他东西?我该如何覆盖它? - rockin'
1
有的。如果您的配置文件中没有特定的绑定部分,并且它未在通过bindingConfiguration属性引用的<endpoint>元素中被引用,则绑定的默认值将被使用(MaxReceivedMessageSize属性的默认值为65536)。因此,您需要向配置中添加适当的<binding>部分。 - Tim
有没有服务器端绑定和/或端点配置的示例?我已经卡在那个点了。 - rockin'
@rockin' - 请发布您的服务配置文件。 - Tim
@rockin' - 配置文件看起来没问题。你是如何在客户端创建代理的?能否将这部分代码添加到你的问题中?根据你创建代理的方式,问题可能就出在那里。一旦我看到代码,我很可能就能确定问题所在。 - Tim
显示剩余2条评论

7

我的问题出现在wcf测试客户端上。由于某种原因,它无法生成具有增加的maxReceivedMessageSize的客户端配置。我的解决方法是右键单击“Config file” -> “使用SvcConfigEditor编辑” -> 绑定 -> maxReceivedMessageSize。


在大多数答案中,他们展示了如何在服务器上设置这些设置。但如果仍然出现错误,就像你提到的那样,我们还应该在客户端上配置这些设置。 在我的情况下,消费Web服务的应用程序的app.config一开始是这样的: <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_ICGSIncomingSOAPCalls" /> <binding name="BasicHttpBinding_MyICGSIncomingSOAPCalls" /> </basicHttpBinding> </bindings>我修改后变成了这样(见第二部分): - mihai71
部分2: <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_ICGSIncomingSOAPCalls" maxReceivedMessageSize="10485760" maxBufferPoolSize="10485760" maxBufferSize="10485760" /> <binding name="BasicHttpBinding_MyICGSIncomingSOAPCalls" maxReceivedMessageSize="10485760" maxBufferPoolSize="10485760" maxBufferSize="10485760" /> </basicHttpBinding> </bindings> 并且它工作正常。 - mihai71

6
这个问题可以通过在配置文件的绑定部分添加以下附加绑定节点来解决。
<basicHttpBinding>
  <binding maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
       maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
  </binding>
</basicHttpBinding>

4
欢迎来到StackOverflow。OP在他们的配置文件中有一个非常相似的代码。您的回答比他们的更好在什么方面?您可以通过解释他们之前没有做过的答案来改进您的答案。 - carlosfigueira

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接