23

我遇到了这个我正在尝试处理的疯狂问题。我知道当我们获取大量数据时,我们必须增加客户端 .config 文件的配额,但是如果我的客户端正在向 WCF 服务器发送大量数据,我该怎么办?

当我发送小型输入参数时,它工作正常。不幸的是,当输入变大时,它就会崩溃。

调试器说:

错误请求,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();
4

3 回答 3

46

您可以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 属性中使用该名称。

于 2011-12-05T08:03:30.743 回答
7

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

于 2014-10-24T00:37:13.090 回答
5

可以通过将以下附加绑定节点添加到配置文件的绑定部分来解决此问题。

<basicHttpBinding>
  <binding maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
       maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
  </binding>
</basicHttpBinding>
于 2013-08-06T22:31:56.747 回答