请求实体太大 WCF REST 服务
Posted
技术标签:
【中文标题】请求实体太大 WCF REST 服务【英文标题】:Request Entity Too Large WCF REST Servcie 【发布时间】:2019-10-07 18:50:30 【问题描述】:您好,我在上传 (188KB 和 5 KB)的小文件时收到“请求实体太大”错误!。我添加了 maxAllowedContentLength ,maxBufferPoolSize 最大字符串内容长度, 最大数组长度, maxBytesPerRead 但仍然有错误 - 这是我的配置文件。
我添加的任何内容似乎都无法解决该错误。 我将上述参数的值设置为非常大的数字。
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Server=SHOUSHOUPC\SQL2016; Initial Catalog=db; User ID=sa; Password=123456; Connect Timeout=10000; pooling='true'; Max Pool Size=200"/>
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
<add key="HOST" value=""/>
<add key="SID" value=""/>
<add key="UserID" value=""/>
<add key="Password" value=""/>
<add key="CC" value=""/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6"/>
<httpRuntime targetFramework="4.5" enableVersionHeader="false" maxRequestLength="2097151"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
<sessionState timeout="2"/>
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="MOH.MOHServices">
<endpoint behaviorConfiguration="webHttpServiceBehavior" binding="webHttpBinding"
contract="MOH.IMOHServices" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpServiceBehavior">
<!-- Important this is the behavior that makes a normal WCF service to REST based service-->
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceAuthorization serviceAuthorizationManagerType="MOH.RestAuthorizationManager, MOH"/>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="16" maxConcurrentInstances="1000" maxConcurrentSessions="10"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<security>
<requestFiltering>
<verbs>
<add verb="OPTIONS" allowed="false"/>
<add verb="TRACE" allowed="false"/>
<add verb="HEAD" allowed="false"/>
</verbs>
<requestLimits maxAllowedContentLength="2000000000" maxUrl="4096" maxQueryString="9999999"/>
</requestFiltering>
</security>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/>
</modules>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="false"/>
<validation validateIntegratedModeConfiguration="false"/>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By"/>
<add name="X-XSS-Protection" value="1; mode=block"/>
<add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains; preload"/>
<add name="X-Frame-Options" value="DENY"/>
<add name="X-Content-Type-Options" value="nosniff"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
服务的痕迹:
跟踪显示以下消息: 已超出传入邮件 (65536) 的最大邮件大小配额。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。
【问题讨论】:
尝试启用日志记录以查看服务的确切问题 @mahlatse 谢谢,但是怎么做呢? 网上应该有几篇文章,但最快的方法是右键单击您的 app.config 文件并选择编辑 WCF 配置并查找高级诊断部分, @mahlatse 请检查跟踪 &检查导致您遇到的安全错误的原因 【参考方案1】:哥们,如果你想创建Restful风格的web服务,我们修改webhttpbinding配置,使用webhttpbinding部分而不是basichttpbinding部分。
<bindings> <basicHttpBinding> <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text"> <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> </binding> </basicHttpBinding> </bindings>
这是WCF4.0新特性提供的简化配置。
<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"/>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" bindingConfiguration="httpbinding" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
另外,主要原因是MaxReceivedMessageSize属性引起的,你也可以只设置这个属性。 如果有什么可以帮助的,请随时告诉我。
【讨论】:
虽然我自己想通了并添加了 webhttpbinding,但我会接受它作为答案。非常感谢。 我有一个问题,调用服务的客户端应用不需要配置任何东西,对吧? 理论上客户端不需要设置相关配置,但我不小心遇到了一些奇怪的问题,可以通过客户端配置来解决。 客户端没有做任何配置,一切正常,这正常吗?以上是关于请求实体太大 WCF REST 服务的主要内容,如果未能解决你的问题,请参考以下文章
将 JSON 发送到 C# WCF 服务会导致:请求实体太大
WCF 413 请求实体太大 - 自托管 WebHttpBinding