大型 WCF Web 服务请求因 (400) HTTP 错误请求而失败 [英] Large WCF web service request failing with (400) HTTP Bad Request

查看:41
本文介绍了大型 WCF Web 服务请求因 (400) HTTP 错误请求而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个看似常见的问题,但一直无法解决.

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

如果我使用数组参数中相对较少的项目(我测试了多达 50 个)调用我的 WCF 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:远程服务器返回意外响应:(400) 错误请求.--->System.Net.WebException:远程服务器返回错误:(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 部分是:

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>

看了一个相当大 数量 答案 这个问题 没有成功.

谁能帮我解决这个问题?

Can anyone help me with 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:

在客户端使用诸如 Fiddler 之类的 HTTP 调试工具来检查 HTTP交通.

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

这篇关于大型 WCF Web 服务请求因 (400) HTTP 错误请求而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆