大WCF Web服务请求与(400)HTTP错误的请求失败错误、Web、WCF、HTTP

2023-09-02 01:24:35 作者:黑白棋局

我遇到这个显然是普遍的问题,一直未能解决。

I've encountered this apparently common problem and have been unable to resolve it.

如果我打电话给我的WCF Web服务使用数量相对较少的数组中的参数项(我测试过多达50个)的,一切都很好。

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

有趣的是,我已经运行 Wireshark的在服务器上,看来,请求甚至没有触及服务器 - 400被在客户端生成的错误。

Interestingly, I've run Wireshark on the server and it appears that the request isn't even hitting the server - the 400 error is being generated on the client side.

的例外是:

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 部分:

The system.serviceModel section of my client config file is:

<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 部分:

On the server side, my web.config file has the following system.serviceModel section:

<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在一个fairly大 number的answers到this问题与没有成功。

谁能帮助我?

推荐答案

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

Try setting maxReceivedMessageSize on the server too, e.g. to 4MB:

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

主要原因默认(65535我相信)是如此之低,是减少拒绝服务(DoS)攻击的风险。需要设置它比服务器上的最大请求大小,并在客户端上的最大响应大小更大。如果你是在Intranet环境,DoS攻击的风险的攻击,可能是低的,所以它可能是安全使用的值比预期需要的要高得多。

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.

顺便说一对连接到WCF服务,故障排除提示:

By the way a couple of tips for troubleshooting problems connecting to WCF services:

启用在这个MSDN文章描述跟踪服务器上。

使用一个HTTP调试工具,如提琴手在客户端上,以检查HTTP流量。

Use an HTTP debugging tool such as Fiddler on the client to inspect the HTTP traffic.