Search code examples
c#restweb-serviceswcf

Request Entity Too Large WCF REST Servcie


Hello I am receiving "Request Entity Too Large" error, when uploading even small files of size (188KB and 5 KB)!. I added maxAllowedContentLength ,maxBufferPoolSize maxStringContentLength, maxArrayLength, maxBytesPerRead but still have the error – This is my config file.

Nothing of what I added seems to resolve the error. I set the values of the parameters above to very large numbers.

    <?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <remove name="LocalSqlServer"/>
    <add name="LocalSqlServer" connectionString="Server=SHOUSHOUPC\SQL2016; Initial Catalog=db; User ID=sa; Password=123456; Connect Timeout=10000; pooling='true'; Max Pool Size=200"/>
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
    <add key="HOST" value=""/>
    <add key="SID" value=""/>
    <add key="UserID" value=""/>
    <add key="Password" value=""/>
    <add key="CC" value=""/>
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.6"/>
    <httpRuntime targetFramework="4.5" enableVersionHeader="false" maxRequestLength="2097151"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
    <sessionState timeout="2"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="MOH.MOHServices">
        <endpoint behaviorConfiguration="webHttpServiceBehavior" binding="webHttpBinding"
          contract="MOH.IMOHServices" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpServiceBehavior">
          <!-- Important this is the behavior that makes a normal WCF service to REST based service-->
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceAuthorization serviceAuthorizationManagerType="MOH.RestAuthorizationManager, MOH"/>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceThrottling maxConcurrentCalls="16" maxConcurrentInstances="1000" maxConcurrentSessions="10"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

  <system.webServer>
    <security>
      <requestFiltering>
        <verbs>
          <add verb="OPTIONS" allowed="false"/>
          <add verb="TRACE" allowed="false"/>
          <add verb="HEAD" allowed="false"/>
        </verbs>
        <requestLimits maxAllowedContentLength="2000000000" maxUrl="4096" maxQueryString="9999999"/>
      </requestFiltering>
    </security>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/>
    </modules>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="false"/>
    <validation validateIntegratedModeConfiguration="false"/>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By"/>
        <add name="X-XSS-Protection" value="1; mode=block"/>
        <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains; preload"/>
        <add name="X-Frame-Options" value="DENY"/>
        <add name="X-Content-Type-Options" value="nosniff"/>
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

The trace of the service:

enter image description here

The trace is showing the following message: The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.


Solution

  • Buddy, if you want to create the Restful style web service, we modify the webhttpbinding configuration by using webhttpbinding section instead of basichttpbinding section.

    <bindings>
      <basicHttpBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    

    Here is a simplified configuration, which provided by the WCF4.0 new feature.

    <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" name="httpbinding">
              <security mode="None">
              </security>
              <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
          <add binding="webHttpBinding" scheme="http" bindingConfiguration="httpbinding" />
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Besides, the main reason is caused by the MaxReceivedMessageSize property, you can also just set this property. Feel free to let me know if there is anything I can help with.