C# 大型 WCF Web 服务请求因 (400) HTTP 错误请求而失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/784606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Large WCF web service request failing with (400) HTTP Bad Request
提问by Damovisa
I've encountered this apparently common problem and have been unable to resolve it.
我遇到了这个明显常见的问题,但一直无解决。
If I call my WCF web service with a relatively small number of items in an array parameter (I've tested up to 50), everything is fine.
如果我使用数组参数中相对较少的项目(我已经测试了 50 个)来调用我的 WCF Web 服务,那么一切都很好。
However if I call the web service with 500 items, I get the Bad Request error.
但是,如果我使用 500 个项目调用 Web 服务,则会收到错误请求错误。
Interestingly, I've run Wiresharkon the server and it appears that the request isn't even hitting the server - the 400 error is being generated on the client side.
有趣的是,我在服务器上运行了Wireshark,看起来请求甚至没有到达服务器 - 400 错误是在客户端生成的。
The exception is:
例外是:
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.
The system.serviceModel
section of my client config file is:
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>
On the server side, my web.config file has the following system.serviceModel
section:
在服务器端,我的 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>
I've looked ata fairly largenumberof answersto this questionwith no success.
Can anyone help me with this?
谁能帮我这个?
采纳答案by Joe
Try setting maxReceivedMessageSize on the server too, e.g. to 4MB:
也尝试在服务器上设置 maxReceivedMessageSize,例如设置为 4MB:
<binding name="MyService.MyServiceBinding"
maxReceivedMessageSize="4194304">
The main reason the default (65535 I believe) is so low is to reduce the risk of Denial of Service (DoS) attacks. You need to set it bigger than the maximum request size on the server, and the maximum response size on the client. If you're in an Intranet environment, the risk of DoS attacks is probably low, so it's probably safe to use a value much higher than you expect to need.
默认值(我认为是 65535)如此之低的主要原因是为了降低拒绝服务 (DoS) 攻击的风险。您需要将其设置为大于服务器上的最大请求大小和客户端上的最大响应大小。如果您处于 Intranet 环境中,DoS 攻击的风险可能很低,因此使用比您预期需要的值高得多的值可能是安全的。
By the way a couple of tips for troubleshooting problems connecting to WCF services:
顺便说一下,解决连接到 WCF 服务的问题的一些技巧:
Enable tracing on the server as described in this MSDN article.
Use an HTTP debugging tool such as Fiddleron the client to inspect the HTTP traffic.
如这篇 MSDN 文章中所述,在服务器上启用跟踪。
在客户端使用Fiddler等HTTP 调试工具检查 HTTP 流量。
回答by Brian
It might be useful to debug the client, turn off Tools\Options\Debugging\General\'Enable Just My Code', click Debug\Exceptions\'catch all first-chance exceptions' for managed CLR exceptions, and see if there is an exception under-the-hood on the client before the protocol exception and before the message hits the wire. (My guess would be some kind of serialization failure.)
调试客户端可能很有用,关闭 Tools\Options\Debugging\General\'Enable Just My Code',单击 Debug\Exceptions\'catch all first-chance exceptions' 以获取托管 CLR 异常,然后查看是否存在在协议异常之前和消息到达线路之前客户端上的异常。(我的猜测是某种序列化失败。)
回答by Mark Davies
I was also getting this issue also however none of the above worked for me as I was using a custom binding (for BinaryXML) after an long time digging I found the answer here :-
我也遇到了这个问题,但是上面没有一个对我有用,因为我在长时间挖掘后使用了自定义绑定(用于 BinaryXML)我在这里找到了答案:-
Sending large XML from Silverlight to WCF
As am using a customBinding, the maxReceivedMessageSize has to be set on the httpTransport element under the binding element in the web.config:
在使用 customBinding 时,必须在 web.config 中 binding 元素下的 httpTransport 元素上设置 maxReceivedMessageSize:
<httpsTransport maxReceivedMessageSize="4194304" />
回答by user469104
For what it is worth, an additional consideration when using .NET 4.0 is that if a valid endpoint is not found in your configuration, a default endpoint will be automatically created and used.
值得一提的是,使用 .NET 4.0 时的另一个注意事项是,如果在您的配置中找不到有效端点,则会自动创建和使用默认端点。
The default endpoint will use all default values so if you think you have a valid service configuration with a large value for maxReceivedMessageSize etc., but there is something wrong with the configuration, you would still get the 400 Bad Request since a default endpoint would be created and used.
默认端点将使用所有默认值,因此如果您认为您有一个有效的服务配置,其中 maxReceivedMessageSize 等值较大,但配置有问题,您仍然会收到 400 Bad Request,因为默认端点将是创建和使用。
This is done silently so it is hard to detect. You will see messages to this effect (e.g. 'No Endpoint found for Service, creating Default Endpoint' or similar) if you turn on tracing on the server but there is no other indication (to my knowledge).
这是静默完成的,因此很难被发现。如果您在服务器上打开跟踪但没有其他指示(据我所知),您将看到有关此效果的消息(例如“未找到服务端点,创建默认端点”或类似信息)。
回答by Yuan
回答by RaSor
In the server in .NET 4.0 in web.config you also need to change in the default binding. Set the follwowing 3 parms:
在 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>
回答by singh
I found the answer to the Bad Request 400 problem.
我找到了错误请求 400 问题的答案。
It was the default server binding setting. You would need to add to server and client default setting.
这是默认的服务器绑定设置。您需要添加到服务器和客户端的默认设置。
binding name="" openTimeout="00:10:00" closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">
绑定名称="" openTimeout="00:10:00" closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647 " maxBufferSize="2147483647">
回答by Aaron Hoffman
You can also turn on WCF logging for more information about the original error. This helped me solve this problem.
您还可以打开 WCF 日志记录以获取有关原始错误的更多信息。这帮助我解决了这个问题。
Add the following to your web.config, it saves the log to C:\log\Traces.svclog
将以下内容添加到您的 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>
回答by Sukhdeep Singh
In my case, it was not working even after trying all solutions and setting all limits to max. In last I found out that a Microsoft IIS filtering module Url Scan 3.1was installed on IIS/website, which have it's own limit to reject incoming requests based on content size and return "404 Not found page".
就我而言,即使在尝试了所有解决方案并将所有限制设置为最大值后,它也无正常工作。最后我发现IIS/网站上安装了一个Microsoft IIS过滤模块Url Scan 3.1,它有自己的限制,可以根据内容大小拒绝传入请求并返回“404 Not found page”。
It's limit can be updated in %windir%\System32\inetsrv\urlscan\UrlScan.ini
file by setting MaxAllowedContentLength
to the required value.
它的限制可以%windir%\System32\inetsrv\urlscan\UrlScan.ini
通过设置MaxAllowedContentLength
为所需的值在文件中更新。
For eg. following will allow upto 300 mb requests
例如。以下将允许多达 300 mb 的请求
MaxAllowedContentLength=314572800
MaxAllowedContentLength=314572800
Hope it will help someone!
希望它会帮助某人!