当我通过 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。
根据您的部署过程,此更改可能会在下一个版本中丢失,因此值得考虑在源代码管理中更新您的配置。
关于c# - 文件上传,远程服务器返回错误 : (413) Request Entity Too Large,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24930058/