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

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

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

关于这个异常有很多问答。但是,它们都没有解决我的问题。

我使用的是 Windows Web Server 2008 R2、IIS 7.5。我的客户端和 WCF 应用程序托管在 IIS 中。我正在使用 channel 调用 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。

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

enter image description here

关于c# - 文件上传,远程服务器返回错误 : (413) Request Entity Too Large,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24930058/

相关文章:

c# - 在 Xamarin.Forms 中使用 xaml 以大写形式显示标签文本

c# - 寻找 OO 大师,在设计我的编程逻辑时需要一些帮助。没什么特别的,只是新的

c# - SignalR 页面刷新建立多重连接

wcf - 连接 WCF 客户端缓存?

c# - 有没有比 DotNetZip 更快的 c# zip 库

c# - 使用 iTextSharp 设置单元格对齐方式和(备用)背景行颜色

c# - ModalPopUpExtender 内的 Gridview 导致 ModalPopUpExtender 关闭

asp.net - ASP.NET MVC 5 中的自定义文件夹结构

c# - WCF 服务导出 1 个接口(interface)和 2 个实现类

c# - 在启动 WCF 服务时调用方法