我需要深入了解在哪里进一步查找与我收到的有关请求实体太大(错误 413)的 WCF 错误相关的信息。
基本上,该服务是一个简单的 [OperationContract],接受字符串作为参数。
<IService.cs>
[OperationContract]
string UploadReportText(string ReportText);
<Service.cs>
public string UploadReportText(string ReportText)
{
// Code to process data passed.
}
我已经为该服务设置了 Web 配置,如下所示:
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</webHttpBinding>
</bindings>
虽然我相信IIS中的uploadReadAhead值不需要修改(因为我没有使用SSL),但我仍然将其修改为2147483647。
在 Chrome 中跟踪调用该服务的应用程序之一,我可以看到数据“Content-Length”为 169786。
我真的很困惑在哪里可以进一步寻找与此相关的内容。
如果我将传递给服务的字符串数据设置为较小的长度,我不会收到错误。我所做的大部分搜索都与此相关,都指向 maxReceivedMessageSize 需要调整为最大可能值,但在 Web 配置中设置它似乎没有任何效果。
我启用了日志记录,并收到此消息:
异常详情:
System.ServiceModel.ProtocolException: The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.
首先:在服务器端,您定义具有较大消息大小的绑定配置,但不从端点引用它。
<service behaviorConfiguration="WCFReferrals.Service1Behavior"
name="WCFReferrals.Referrals">
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="LargeSizeMessages" <== you need to reference the binding config (by specifying its name
contract="WCFReferrals.IReferrals">
</endpoint>
.....
</service>
....
<binding name="LargeSizeMessages" <== give it a meaningful name
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
</binding>
正如 Vignesh 所说,您没有为定义的绑定分配名称。 这使其成为该绑定的默认配置(在 WCF 4.0+ 更高版本中),因此您实际上有两个选择。 您可以根据 Vignesh 的建议为其命名,创建显式端点并通过
bindingCongifuration
引用它。
或者,您可以使用
<proctolMapping>
部分的 <system.serviceModel>
部分将 webHttpBinding
指定为 http
的默认绑定(http 的正常 WCF 默认绑定是 basicHttpBinding
:
<system.serviceModel>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocoalMapping>
</system.serviceModel>
这会出现在您服务的配置文件中。
对我来说,我只需要将这个块添加到我的web.config:
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="2147483647"
maxBufferPoolSize="2147483647777" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</webHttpBinding>
</bindings>
我的web.config已经使用了
webHttpBinding
绑定(如下所示),它只需要这个<bindings>
部分来允许它上传大文件。
<services>
<service name="PocketCRMServices.Service1">
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="PocketCRMServices.IService1"
behaviorConfiguration="webBehaviour" />
</service>
</services>
# In my case I had the following configuration.
- Four final points.
-
<endpoint address="rest" binding="webHttpBinding"
contract="MSService.IService"
behaviorConfiguration="web"
bindingConfiguration="webhttpsBinding"/>
<endpoint address="" binding="basicHttpBinding" name="Service"
contract="MSService.IService" />
<endpoint address="" binding="basicHttpsBinding" name="Service"
contract="MSService.IService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
- A definite link
-
<webHttpBinding>
<binding name="webhttpsBinding">
<security mode="Transport" />
</binding>
</webHttpBinding>
- A protocolMapping section
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
I set up the only bindig, with
maxReceivedMessageSize="2147483647" maxBufferSize="2147483647 maxBufferPoolSize="2147483647
And finally I added the readeQuotas section
<readerQuotas maxBytesPerRead="2147483647" maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647"/>
My configuration is:
<bindings><webHttpBinding>
<binding name="webhttpsBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
<readerQuotas maxBytesPerRead="2147483647" maxArrayLength="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
您还需要在客户端执行相同的操作。
您的客户端配置(
app.config
还包括大消息大小绑定配置吗?