0
votes

I designed a simple WCF service that allows users upload files. I also created a ASP.NET website to adopt the service. However, I keep receiving the error message "The remote server returned an unexpected response:

(413) Request Entity Too Large

I have tried to search MSDN and StackOverflow to seek solutions, but none of them works.

The web.config of my WCF service

<?xml version="1.0"?>
<configuration>
   <appSettings>
      <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
   </appSettings>
   <system.web>
       <compilation debug="true" targetFramework="4.5" />
       <httpRuntime targetFramework="4.5"/>
   </system.web>
   <system.serviceModel>
      <bindings>
         <basicHttpBinding>
            <binding name="bindingMetadata" maxBufferPoolSize="2147483647">
               <readerQuotas maxDepth="256" maxStringContentLength="2147483647" 
                      maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                      maxNameTableCharCount="65536" />
            </binding>
         </basicHttpBinding>
      </bindings>
      <services>
         <service name="ServiceStorage.ServiceStorage" 
                  behaviorConfiguration="behaviorMetadata">
            <endpoint
                address=""
                binding="basicHttpBinding"
                bindingConfiguration="bindingMetadata"
                contract="ServiceStorage.IService"/>
            <endpoint
                address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange"/>
         </service>
      </services>
      <behaviors>
         <serviceBehaviors>
             <behavior name="behaviorMetadata">
                 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                 <serviceDebug includeExceptionDetailInFaults="true"/>
                 <dataContractSerializer maxItemsInObjectGraph="2147483647" />
             </behavior>
         </serviceBehaviors>
         <endpointBehaviors>
             <behavior>
                  <dataContractSerializer maxItemsInObjectGraph="2147483647" />
             </behavior>
         </endpointBehaviors>
      </behaviors>
      <protocolMapping>
          <add binding="basicHttpsBinding" scheme="https" />
      </protocolMapping>    
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                                 multipleSiteBindingsEnabled="true" />
   </system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        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="true"/>
    <security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647" />
    </requestFiltering>
    </security>
</system.webServer>

</configuration>

The Web.config of ASP.NET website

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-TestServiceStorage-01f455a2-2fb1-42f0-9099-18bc95b05403;AttachDbFilename=|DataDirectory|\aspnet-TestServiceStorage-01f455a2-2fb1-42f0-9099-18bc95b05403.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5" maxRequestLength="2097150"/>
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization"/>
        <add namespace="Microsoft.AspNet.Identity"/>
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
      </controls>
    </pages>
    <membership>
      <providers>
        <!--
        ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
        <clear/>
      </providers>
    </membership>
    <profile>
      <providers>
        <!--
        ASP.NET Membership Profile is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
        <clear/>
      </providers>
    </profile>
    <roleManager>
      <!--
          ASP.NET Membership Role is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
      <providers>
        <clear/>
      </providers>
    </roleManager>
    <!--
            If you are deploying to a cloud environment that has multiple web server instances,
            you should change session state mode from "InProc" to "Custom". In addition,
            change the connection string named "DefaultConnection" to connect to an instance
            of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
      -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"/>
      </providers>
    </sessionState>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" maxBufferSize="1073741824" maxReceivedMessageSize="1073741824" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:65001/ServiceStorage.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
        contract="ServiceStorage.IService" name="BasicHttpBinding_IService" />
    </client>
  </system.serviceModel>
</configuration>

Does anyone have any idea? Thanks,

1

1 Answers

0
votes

Did you try this solution?

WCF by default limits messages to 65KB to avoid denial of service attack with large messages. [...] To solve this issue you must reconfigure your service to accept larger messages.

You need to set maxReceivedMessageSize in your binding. You can also need to set readerQuotas.

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxReceivedMessageSize="10485760">
        <readerQuotas ... />
      </binding>
    </basicHttpBinding>
  </bindings>  
</system.serviceModel>