c# - WCF 错误:远程服务器返回错误:(413)请求实体太大
问题描述
我创建了一个 WCF 服务来将 1MB 的文件数据上传到 Windows Server。此 Web 服务在处理小文件 (950 KB) 数据时运行良好,但文件较大时存在问题。
我尝试了几种方法在 WCF web.config 中修复它,但仍然陷入上述错误。我需要帮助设置 WCF 配置来解决我的问题。
这是我的 web.config 如下:
<system.serviceModel>
<services>
<service name="WCFService.UploadService">
<endpoint address="REST" behaviorConfiguration="WCFServiceBehavior" binding="webHttpBinding" bindingConfiguration="WCFServiceBinding" contract="WCFService.IUploadService" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="WCFServiceBinding" maxReceivedMessageSize="1073741824" maxBufferSize="20971520" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transferMode="Streamed">
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="WCFServiceBehavior">
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
</system.serviceModel>
请在 WCF web.config 上方更正
解决方案
请参考下面的配置,它同时支持 Http 和 Https。你可以根据需要剪掉它。此外,我使用 ProtocolMapping 功能来简化我的配置。
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" name="httpbinding">
<security mode="None">
</security>
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
<binding name="httpsbinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<!--http and https are all supported.-->
<add binding="webHttpBinding" scheme="http" bindingConfiguration="httpbinding"/>
<add binding="webHttpBinding" scheme="https" bindingConfiguration="httpsbinding"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
对于如何简化配置,
https:
//docs.microsoft.com/en-us/dotnet/framework/wcf/simplified-configuration 如果问题仍然存在,请随时告诉我。
推荐阅读
- kotlin - Kotlin 内联函数只有一个 return 语句
- html - github 自述文件中居中的表情符号
- powerbi - 日期之间的 DAX SUM 未按预期工作
- python - 如何读取 matchTemplate 的结果(python 中的 OpenCV2)
- python - 代码不会打印多个列表,但没有显示错误
- php - 页面加载时,我无法显示带有值的动态输入字段?
- powerbi - 如何在 Power BI 中创建动态日历
- node.js - NodeJS 上传到谷歌存储
- java - 如何在android中更改图像的色调、饱和度和亮度
- rust - 如何使用 Tokio 远程关闭正在运行的任务