首页 文章

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

提问于
浏览
429

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

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

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

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

13 回答

  • 87

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

    <bindings>
        <basicHttpBinding>
            <binding name="basicHttp" allowCookies="true"
                     maxReceivedMessageSize="20000000" 
                     maxBufferSize="20000000"
                     maxBufferPoolSize="20000000">
                <readerQuotas maxDepth="32" 
                     maxArrayLength="200000000"
                     maxStringContentLength="200000000"/>
            </binding>
        </basicHttpBinding>
    </bindings>
    

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

    ...
    bindingConfiguration="basicHttp"
    ...
    

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

  • 4

    如果你're still getting this error message while using the WCF Test Client, it'因为客户端有一个单独的 MaxBufferSize 设置 .

    To correct the issue:

    • 右键单击树底部的 Config File 节点

    • 选择使用SvcConfigEditor编辑

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

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

  • 6

    如果要动态创建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;
    
  • 39

    WCF Test Client 拥有自己的客户端配置 .

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

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

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

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

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

  • 559

    我找到了简单的方法

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

  • 138

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

    <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>
    
  • 3
    <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>
    
  • 6

    我在我的项目中使用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>
    
  • 3

    Another important thing to consider from my experience..

    我强烈建议不要最大化maxBufferPoolSize,因为池中的缓冲区永远不会释放,直到app-domain(即应用程序池)循环使用 .

    高流量时段可能会导致大量内存被使用而且从未被释放 .

    更多细节在这里:

  • 18

    不要忘记执行入口点的app.config将被考虑,而不是管理Web服务调用的类库项目中的那个(如果有) .

    例如,如果在运行单元测试时遇到错误,则需要在测试项目中设置适当的配置 .

  • 8

    对我来说,我所要做的就是将 maxReceivedMessageSize="2147483647" 添加到客户端app.config中 . 服务器没有动过 .

  • 2

    对于 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>
    

    对于 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>
    

    IMPORTANT:

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

    <behaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior">
          ...
          <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  • 1

    我在web.config上使用此设置时出现此错误

    System.ServiceModel.ServiceActivationException
    

    我设置这样的设置:

    <service name="idst.Controllers.wcf.Service_Talks">
        <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_TalksAspNetAjaxBehavior"
          binding="webHttpBinding" contract="idst.Controllers.wcf.Service_Talks" />
      </service>
      <service name="idst.Controllers.wcf.Service_Project">
        <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_ProjectAspNetAjaxBehavior"
          binding="basicHttpBinding" bindingConfiguration="" bindingName="largBasicHttp"
          contract="idst.Controllers.wcf.Service_Project" />
      </service>
    </services>
    
    <bindings>
    <basicHttpBinding>
        <binding name="largBasicHttp" allowCookies="true"
                 maxReceivedMessageSize="20000000"
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
            <readerQuotas maxDepth="32"
                 maxArrayLength="200000000"
                 maxStringContentLength="200000000"/>
        </binding>
    </basicHttpBinding>
    

相关问题