大型WCF Web服务请求失败(400)HTTP错误请求如何解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (138)

我遇到了这个显而易见的常见问题,并且无法解决它。

如果我使用数组参数中相对较少数量的项目调用我的WCF Web服务(我已经测试了多达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.

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服务的疑难解答提示:

用户回答回答于

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

<httpsTransport maxReceivedMessageSize="4194304" /> 

扫码关注云+社区