【问题标题】:File Upload, The remote server returned an error: (413) Request Entity Too Large文件上传,远程服务器返回错误:(413) Request Entity Too Large
【发布时间】:2014-09-15 19:01:44
【问题描述】:

当我通过 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 上传大文件?

谢谢。

【问题讨论】:

    标签: c# asp.net wcf iis file-upload


    【解决方案1】:

    您可能需要在 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。

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

    【讨论】:

    • 我注意到您谈到更新客户端 web.config,客户端 Web.Config 发出请求,但服务器是请求过滤发生的地方,因此更改客户端 web.config 的请求限制不会有任何影响。
    • 另外,我会考虑使用 WCF 流,它可以防止文件存储在 RAM 中。我用它来传输没有这些问题的大文件。
    • 我更改了客户端 web.config 和 WCF 应用程序 web.config。但是没有效果。
    • 最后,我开始使用 Windows 服务来托管 WCF。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多