大型WCF Web服务请求失败(400)HTTP错误请求

我遇到了这个显然是常见的问题,一直无法解决它。

如果我调用我的WCF Web服务与一个数组中的相对较less数量的项目(我已经testing了50),一切都很好。

但是,如果我用500个项目调用Web服务,我得到错误的请求错误。

有趣的是,我已经在服务器上运行了Wireshark ,看起来这个请求甚至没有到达服务器 – 客户端正在生成400错误。

例外是:

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. 

我的客户端configuration文件的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> 

我已经看了 这个问题的相当 多的答案 , 但 没有成功 。

谁能帮我这个?

尝试在服务器上设置maxReceivedMessageSize,例如4MB:

  <binding name="MyService.MyServiceBinding" maxReceivedMessageSize="4194304"> 

默认(65535我相信)的主要原因是为了降低拒绝服务(DoS)攻击的风险。 您需要将其设置为大于服务器上的最大请求大小以及客户端上的最大响应大小。 如果您处于Intranet环境中,DoS攻击的风险可能很低,因此使用比您预期的要高得多的值可能是安全的。

顺便提一些关于连接到WCF服务的疑难解答提示:

  • 按照此MSDN文章中所述启用服务器上的跟踪。

  • 在客户端上使用诸如Fiddler之类的HTTPdebugging工具来检查HTTPstream量。

我也遇到了这个问题,但也没有上述工作对我来说,因为我使用了一个自定义绑定(BinaryXML)很长一段时间挖后,我在这里find答案: –

从Silverlight发送大的XML到WCF

在使用customBinding时,必须在web.config的绑定元素下的httpTransport元素上设置maxReceivedMessageSize:

 <httpsTransport maxReceivedMessageSize="4194304" /> 

在使用.NET 4.0时,如果在configuration中没有find有效的端点,则会自动创build和使用默认的端点。

默认端点将使用所有的默认值,所以如果你认为你有一个有效的服务configuration,对于maxReceivedMessageSize等有一个大的值,但是configuration有问题,你仍然会得到400的错误请求,因为默认端点是创build和使用。

这是默默地完成,所以很难察觉。 如果您打开服务器上的跟踪,但是没有其他指示(据我所知),您将看到这样的消息(例如,“找不到服务的端点,创build默认端点”或类似的消息)。

在web.config中的.NET 4.0服务器中,还需要更改默认绑定。 设置以下3个参数:

  < basicHttpBinding> < !--http://www.intertech.com/Blog/post/NET-40-WCF-Default-Bindings.aspx - Enable transfer of large strings with maxBufferSize, maxReceivedMessageSize and maxStringContentLength --> < binding **maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"**> < readerQuotas **maxStringContentLength="2147483647"**/> < /binding> 

debugging客户端,closures工具\选项\debugging\常规\'启用只是我的代码',点击debugging\例外\'捕获所有首次机会例外',对于托pipe的CLR例外可能是有用的,看看是否有一个在协议例外之前和消息触及连线之前,在客户端上的exception情况。 (我的猜测会是某种序列化失败。)

您也可以打开WCF日志logging以获取有关原始错误的更多信息。 这帮助我解决了这个问题。

将以下内容添加到您的web.config中,它将日志保存到C:\ log \ Traces.svclog

 <system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"> <listeners> <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "c:\log\Traces.svclog" /> </listeners> </source> </sources> </system.diagnostics> 

只是想指出

除了MaxRecivedMessageSize之外,在ReaderQuotas下还有一些属性,你可能会遇到一些项目限制而不是大小限制。 MSDN链接在这里

我find了错误请求400问题的答案。

这是默认的服务器绑定设置。 您需要添加到服务器和客户端的默认设置。

binding name =“”openTimeout =“00:10:00”closeTimeout =“00:10:00”receiveTimeout =“00:10:00”sendTimeout =“00:10:00”maxReceivedMessageSize =“2147483647”maxBufferPoolSize =“2147483647 “maxBufferSize =”2147483647“>

就我而言,即使在尝试所有解决scheme并将所有限制设置为最大值后,仍然无法正常工作。 最后,我发现在IIS /网站上安装了一个Microsoft IIS筛选模块Url Scan 3.1 ,它有自己的限制,可以根据内容大小拒绝传入的请求,并返回“404 Not found页面”。

通过将MaxAllowedContentLength设置为所需的值,可以在%windir%\System32\inetsrv\urlscan\UrlScan.ini文件中更新限制。

例如。 以下将允许多达300 MB的请求

MaxAllowedContentLength = 314572800

希望它会帮助别人!