繁体   English   中英

文件上传,远程服务器返回错误:(413) Request Entity Too Large

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

当我通过 WCF 服务上传文件时,收到异常“远程服务器返回错误:(413) 请求实体太大”

关于这个例外有很多问题和答案。 但是,它们都没有解决我的问题。

我使用的是 Windows Web Server 2008 R2、IIS 7.5。 我的客户端和 WCF 应用程序托管在 IIS 中。 我正在使用频道来调用 WCF 服务。

        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配置文件如下:

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

另外,我在客户端 web.config 文件中添加了以下配置:

<httpRuntime maxRequestLength="2097151" />

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

我更改了 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>

但是,它们都没有解决我的问题。 Windows Server 2012没有问题,应该是Windows Server 2008的问题。Windows Server 2008如何用WCF上传大文件?

谢谢。

您可能需要更新 System.WebServer 部分中的 IIS 请求限制,默认值为 30MB。

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

更多信息在这里:

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

您可以使用 IIS 中的配置编辑器执行此操作。

要访问它,请在 IIS 管理器中选择网站,然后在管理部分下选择配置编辑器。 深入到 requestLimits 部分,您可以根据下面的屏幕截图更新那里的配置。 记得点击“应用”。 这将更新应用程序根目录中的 Web.Config。

根据您的部署过程,此更改可能会在下一个版本中丢失,因此值得考虑在源代码管理中更新您的配置。

在此处输入图片说明

我必须将它添加到 system.web 部分中的 web.config(注意:不是 system.webserver)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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