WCF - How to Increase Message Size Quota

后端 未结 13 1807
失恋的感觉
失恋的感觉 2020-11-22 06:16

I have a WCF Service which returns 1000 records from database to the client. I have an ASP.NET WCF client (I have added service reference in asp.net web application project

相关标签:
13条回答
  • 2020-11-22 06:51

    I solved my issue on Bing Maps WPF on my project Using CalculateRoute (). The solution in my case was setting the maxReceivedMessageSize and maxReceivedMessageSize on attribute "httpTransport" for "customBinding" section .

    I set in the applications.config file (es. myApp.config) this configuration:

    <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>
    
    0 讨论(0)
  • 2020-11-22 06:59

    If you're still getting this error message while using the WCF Test Client, it's because the client has a separate MaxBufferSize setting.

    To correct the issue:

    1. Right-Click on the Config File node at the bottom of the tree
    2. Select Edit with SvcConfigEditor

    A list of editable settings will appear, including MaxBufferSize.

    Note: Auto-generated proxy clients also set MaxBufferSize to 65536 by default.

    0 讨论(0)
  • 2020-11-22 07:02

    The WCF Test Client has it's own client config.

    Run the test client and scroll to the bottom. If you double click the Config File node you will see the XML representation. As you can see the maxReceivedMessageSize is 65536.

    To edit this, Right Click the Config File tree node and select Edit With SvcConfigEditor. When the editor opens expand Bindings and double click the binding that was automatically generated.

    You can edit all the properties here, including maxReceivedMessageSize. When you are done click File - Save.

    Lastly, when you are back at the WCF Test Client window, click Tools - Options.

    NOTE: Uncheck the Always regenerate config when launching services.

    0 讨论(0)
  • 2020-11-22 07:03

    If you are creating your WCF bindings dynamically here's the code to use:

    BasicHttpBinding httpBinding = new BasicHttpBinding();
    httpBinding.MaxReceivedMessageSize = Int32.MaxValue;
    httpBinding.MaxBufferSize = Int32.MaxValue;
    // Commented next statement since it is not required
    // httpBinding.MaxBufferPoolSize = Int32.MaxValue;
    
    0 讨论(0)
  • 2020-11-22 07:03

    For 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>
    

    For 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:

    If you try to pass complex object that has many connected objects (e.g: a tree data structure, a list that has many objects...), the communication will fail no matter how you increased the Quotas. In such cases, you must increase the containing objects count:

    <behaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior">
          ...
          <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    0 讨论(0)
  • 2020-11-22 07:07

    Don't forget that the app.config of the execution entry point will be considered, not the one in class library project managing Web-Service calls if there is one.

    For example if you get the error while running unit test, you need to set up appropriate config in the testing project.

    0 讨论(0)
提交回复
热议问题