简体   繁体   中英

File Upload, The remote server returned an error: (413) Request Entity Too Large

When I upload a file via WCF services, I received the exception, "The remote server returned an error: (413) Request Entity Too Large"

There are a lot of questions and answers about this exception. But, none of them is solved my problem.

I'm using Windows Web Server 2008 R2, IIS 7.5. My client and WCF applications are hosted in IIS. I'm using channel to call WCF service.

        var binding = new BasicHttpBinding();

        binding.MaxReceivedMessageSize = Int32.MaxValue;
        binding.MaxBufferPoolSize = Int32.MaxValue;
        binding.MaxBufferSize = Int32.MaxValue;
        binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
        binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
        binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
        binding.ReaderQuotas.MaxDepth = Int32.MaxValue;
        binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;

        var address = new EndpointAddress("WCF Service URL");

        return ChannelFactory<IFileService>.CreateChannel(binding, address);

WCF configuration file like the following:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behavior1">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="MyProject.WCF.FileService" behaviorConfiguration="behavior1">
        <endpoint binding="basicHttpBinding" bindingName="binding1" contract="MyProject.WCF.Contracts.Service.IFileService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="binding1" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Also, I add the client web.config file the following configurations:

<httpRuntime maxRequestLength="2097151" />

<serverRuntime uploadReadAheadSize="2147483647" />
<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
</security> 

I changed C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config,

<location path="My Site Path" overrideMode="Allow">
    <system.webServer>
        <httpLogging dontLog="true" />
        <serverRuntime uploadReadAheadSize="2147483647" />
    </system.webServer>
</location>

But, none of them is solved my problem. There is no problem with Windows Server 2012. Probably, the problem is related to Windows Server 2008. How can upload large files with WCF in Windows Server 2008?

Thanks.

You probably need to update the IIS request limits in the System.WebServer section, the default is 30MB.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="30000001" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

More information here:

http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

You can do this using the Configuration Editor in IIS.

To access this, select the Website in IIS Manager, then select Configuration Editor, under the Management Section. Drill down into the requestLimits section, and you can update the configuration there, as per the screenshot below. Remember to click "Apply". This will update the Web.Config in the root of the application.

Depending on your deployment process, this change might be lost at the next release, so it's worth considering updating your configurations in source control.

在此处输入图片说明

I had to add this to web.config in the system.web section (NOTE: not system.webserver)

<system.web> < httpRuntime maxRequestLength="100485760" requestValidationMode="2.0" maxQueryStringLength="18192" /> </system.web>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM