WCF - 如何增加邮件大小配额

bug*_*ger 447 .net asp.net wcf .net-3.5

我有一个WCF服务,它从数据库返回1000条记录到客户端.我有一个ASP.NET WCF客户端(我在asp.net Web应用程序项目中添加了服务引用以使用WCF).

运行客户端应用程序时收到以下消息:

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

有帮助吗?如何增加邮件大小配额?

Nat*_*ate 592

您需要这样的东西来增加App.configWeb.config文件中的邮件大小配额:

<bindings>
    <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="20000000" 
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
            <readerQuotas maxDepth="32" 
                 maxArrayLength="200000000"
                 maxStringContentLength="200000000"/>
        </binding>
    </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

并在端点配置中使用绑定名称,例如

...
bindingConfiguration="basicHttp"
...
Run Code Online (Sandbox Code Playgroud)

值的理由很简单,它们足够大以容纳大多数消息.您可以调整该数字以满足您的需求.低默认值基本上是为了防止DOS类型的攻击.使其成为20000000将允许分布式DOS攻击有效,默认大小为64k将需要大量客户端来支持大多数服务器.

  • 谢谢.这个更改需要在客户端应用程序的web.config文件中进行. (19认同)
  • 对于其他感兴趣的人,我在另一篇博客上看到,最大大小是2147483647.20000000比这个数字略小,所以使用最小的数字,你可以在不中断服务的情况下逃脱是有道理的. (18认同)
  • 它足以容纳大多数消息.您可以调整该数字以满足您的需求.它基本上是防止DOS类型攻击.使其成为20000000将允许分布式DOS攻击有效,默认大小为64k将需要非常大量的客户端来支持当今大多数服务器. (9认同)
  • 您可能还需要在服务器上更改它 - 如果您需要将大型数据集作为参数发送到WCF方法. (8认同)
  • @Slauma如果传入参数太大,则需要在服务器上进行更改; 否则(并且更有可能)需要在客户端配置文件中进行更改,因为它是服务(而不是其参数)的响应太大. (5认同)
  • 消息大小为20000000的任何特殊原因? (4认同)
  • 问题中的异常消息(关于服务器上的消息大小*传入*)***需要更改以解决此特定问题的事情是在*服务器的*配置文件中增加`maxReceivedMessageSize`.不需要更改客户端配置也不需要更改任何其他参数. (2认同)

Mic*_*ues 148

如果您在使用WCF测试客户端时仍然收到此错误消息,那是因为客户端具有单独的MaxBufferSize设置.

要纠正此问题:

  1. 右键单击树底部的" 配置文件"节点
  2. 选择使用SvcConfigEditor编辑

将显示可编辑设置列表,包括MaxBufferSize.

注意: 默认情况下,自动生成的代理客户端还将MaxBufferSize设置为65536.

  • 在vs2013上,如果人们正在寻找SvcConfigEditor,则替换为编辑WCF配置. (9认同)
  • 为什么我为什么总是忘记这个?+1 (8认同)

Bha*_*h K 98

如果要动态创建WCF绑定,请使用以下代码:

BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.MaxReceivedMessageSize = Int32.MaxValue;
httpBinding.MaxBufferSize = Int32.MaxValue;
// Commented next statement since it is not required
// httpBinding.MaxBufferPoolSize = Int32.MaxValue;
Run Code Online (Sandbox Code Playgroud)

  • 10年后,我再次需要这个,我希望我能投票两次。 (2认同)

小智 44

WCF测试客户端有它自己的客户端配置.

运行测试客户端并滚动到底部.如果双击"配置文件"节点,您将看到XML表示.正如你所看到的maxReceivedMessageSize65536.

要编辑它,请右键单击"配置文件"树节点,然后选择"编辑方式" SvcConfigEditor.编辑器打开时展开Bindings并双击自动生成的绑定.

您可以在此处编辑所有属性,包括maxReceivedMessageSize.完成后,单击文件 - 保存.

最后,当您返回WCF测试客户端窗口时,单击工具 - 选项.

注意:取消选中启动服务时始终重新生成配置.

  • upvote是因为要取消选中`Always regenerate config`选项. (3认同)
  • 可能是这里最好的答案! (2认同)

小智 23

我找到了简单的方法

---右键单击webconfig或app配置文件,然后单击EDIT WCF CONFIGURATION并选择bingdigs和选择yore服务,右侧显示maxReciveMessageSize给出一个大数字---

  • 这是一个很好的答案,我不知道我可以从这里编辑,谢谢 (2认同)

Raj*_*uri 8

我解决了这个问题......如下

    <bindings>
  <netTcpBinding>
    <binding name="ECMSBindingConfig" closeTimeout="00:10:00" openTimeout="00:10:00"
      sendTimeout="00:10:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
      maxReceivedMessageSize="2147483647" portSharingEnabled="true">
      <readerQuotas maxArrayLength="2147483647" maxNameTableCharCount="2147483647"
          maxStringContentLength="2147483647" maxDepth="2147483647"
          maxBytesPerRead="2147483647" />
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="ECMSServiceBehavior">
      <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceTimeouts transactionTimeout="00:10:00" />
      <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="100"
        maxConcurrentInstances="100" />
    </behavior>
  </serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

  • 这与我的解决方案有何不同?除了你包括配置的所有不相关部分以及相关部分,你选择了最大可能值而不是我选择的200米? (20认同)
  • 上下文也很好......也许这两个答案可以合并? (3认同)

小智 8

我在我的项目中使用CalculateRoute()在Bing Maps WPF上解决了我的问题.我的解决方案是在"customBinding"部分的属性"httpTransport"上设置maxReceivedMessageSize和maxReceivedMessageSize.

我在applications.config文件(es.myApp.config)中设置了这个配置:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IGeocodeService" />
            <binding name="BasicHttpBinding_IRouteService" />
        </basicHttpBinding>
        <customBinding>
            <binding name="CustomBinding_IGeocodeService">
                <binaryMessageEncoding />
              <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                                maxReceivedMessageSize="2147483647" allowCookies="false" authenticationScheme="Anonymous"
                                bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                                keepAliveEnabled="true" maxBufferSize="2147483647" proxyAuthenticationScheme="Anonymous"
                                realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                                useDefaultWebProxy="true" />
            </binding>
            <binding name="CustomBinding_IRouteService">
                <binaryMessageEncoding />
              <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                                maxReceivedMessageSize="2147483647" allowCookies="false" authenticationScheme="Anonymous"
                                bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                                keepAliveEnabled="true" maxBufferSize="2147483647" proxyAuthenticationScheme="Anonymous"
                                realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                                useDefaultWebProxy="true" />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService"
            contract="BingServices.IGeocodeService" name="BasicHttpBinding_IGeocodeService" />
        <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc/binaryHttp"
            binding="customBinding" bindingConfiguration="CustomBinding_IGeocodeService"
            contract="BingServices.IGeocodeService" name="CustomBinding_IGeocodeService" />
        <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRouteService"
            contract="BingServices.IRouteService" name="BasicHttpBinding_IRouteService" />
        <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc/binaryHttp"
            binding="customBinding" bindingConfiguration="CustomBinding_IRouteService"
            contract="BingServices.IRouteService" name="CustomBinding_IRouteService" />
    </client>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)


Hem*_*oni 6

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding_Username" maxReceivedMessageSize="20000000"          maxBufferPoolSize="20000000">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName" establishSecurityContext="false"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<client>
  <endpoint
            binding="wsHttpBinding"
            bindingConfiguration="wsHttpBinding_Username"
            contract="Exchange.Exweb.ExchangeServices.ExchangeServicesGenericProxy.ExchangeServicesType"
            name="ServicesFacadeEndpoint" />
</client>
Run Code Online (Sandbox Code Playgroud)


Val*_*ile 6

根据我的经验,另一个需要考虑的重要事项..

我强烈建议不要最大化 maxBufferPoolSize,因为在应用程序域(即应用程序池)回收之前,池中的缓冲区永远不会被释放。

一段时间的高流量可能会导致大量内存被使用而永远不会释放。

更多细节在这里:


Jac*_*cob 5

对于HTTP:

<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000" 
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="200" 
             maxArrayLength="200000000"
             maxBytesPerRead="4096"
             maxStringContentLength="200000000"
             maxNameTableCharCount="16384"/>
    </binding>
  </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

对于TCP:

<bindings>
  <netTcpBinding>
    <binding name="tcpBinding"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
      <readerQuotas maxDepth="200"
           maxArrayLength="200000000"
           maxStringContentLength="200000000"
           maxBytesPerRead="4096"
           maxNameTableCharCount="16384"/>
    </binding>
  </netTcpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

重要:

如果您尝试传递具有许多连接对象的复杂对象(例如:树数据结构,具有许多对象的列表...),则无论您如何增加配额,通信都会失败。在这种情况下,必须增加包含对象的数量:

<behaviors>
  <serviceBehaviors>
    <behavior name="NewBehavior">
      ...
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)


ash*_*lon 5

对我来说,我要做的就是将其添加maxReceivedMessageSize="2147483647"到客户端app.config中。服务器保持不变。