我遇到了这个明显常见的问题,但一直无法解决。
如果我在数组参数中使用相对较少的项目调用我的 WCF Web 服务(我测试过最多 50 个),一切都很好。
但是,如果我用 500 个项目调用 Web 服务,我会收到 Bad Request 错误。
有趣的是,我运行了 Wireshark在服务器上,看起来请求甚至没有到达服务器 - 在客户端生成 400 错误。
异常(exception)情况是:
System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
我的客户端配置文件的 system.serviceModel
部分是:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IMyService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://serviceserver/MyService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMyService"
contract="SmsSendingService.IMyService" name="WSHttpBinding_IMyService" />
</client>
</system.serviceModel>
在服务器端,我的 web.config 文件具有以下 system.serviceModel
部分:
<system.serviceModel>
<services>
<service name="MyService.MyService" behaviorConfiguration="MyService.MyServiceBehaviour" >
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="MyService.MyServiceBinding" contract="MyService.IMyService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="MyService.MyServiceBinding">
<security mode="None"></security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyService.MyServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
我已经 looked at一个fairly large number的 answers至 this question与 no success .
谁能帮我解决这个问题?
最佳答案
也尝试在服务器上设置 maxReceivedMessageSize,例如到 4MB:
<binding name="MyService.MyServiceBinding" maxReceivedMessageSize="4194304">
默认值(我相信是 65535)如此低的主要原因是为了降低拒绝服务 (DoS) 攻击的风险。您需要将其设置为大于服务器上的最大请求大小和客户端上的最大响应大小。如果您处于 Intranet 环境中,则 DoS 攻击的风险可能很低,因此使用比您预期需要的值高得多的值可能是安全的。
顺便说一下,解决连接到 WCF 服务的问题的一些技巧:
按照 this MSDN article 中的描述在服务器上启用跟踪.
使用 HTTP 调试工具,例如 Fiddler在客户端上检查 HTTP 流量。
关于c# - 大型 WCF Web 服务请求因 (400) HTTP Bad Request 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/784606/