我遇到过这个明显常见的问题,一直无法解决.

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.

However if I call the web service with 500 items, I get the Bad Request error.

有趣的是,我在服务器上运行了Wireshark,看起来请求甚至没有到达服务器-400错误是在客户端生成的.

The exception is:

System.ServiceModel.ProtocolException:远程服务器返回意外响应:(400)错误请求.->System.Net.WebException:远程服务器返回错误:(400)请求错误.

The 100 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>

On the server side, my web.config file has the following 100 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 atfairly largenumberanswersthis questionno success个.

有人能帮我吗?

推荐答案

try 在服务器上也设置maxReceivedMessageSize,例如设置为4MB:

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

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

顺便说一句,关于连接到WCF服务的故障排除,有两个提示:

  • 在服务器上启用跟踪,如this MSDN article中所述.

  • 在客户端上使用HTTP调试工具(如Fiddler)来判断HTTP流量.

.net相关问答推荐

无法在Designer、VS2022、. NET 8中打开WinForms表单'

DotNet COM初始化问题

在本地运行 Azure 函数会在 .NET7 升级后出现无运行时错误

查找所有源硬编码字符串

即时窗口中的动态导致Microsoft.CSharp.RuntimeBinder.Binder未定义或导入错误

将屏幕捕获为位图

如何根据新的安全策略在 .Net 中发送邮箱?

如何找到 ManualResetEvent 的状态?

如何判断对象是否是某种类型的数组?

如何在不丢失数据的情况下重命名 Entity Framework 5 Code First 迁移中的数据库列?

创建多个线程并等待所有线程完成

如何将枚举值序列化为 int?

等效于 C# 在 powershell 中的使用关键字?

一个接口是否应该继承另一个接口

静态方法继承的正确替代方法是什么?

绑定到不在列表中的值的可编辑组合框

如何访问 Session 变量并在 javascript 中设置它们?

如何获取当前的 ProcessID?

System.ServiceModel 在 .NET Core 项目中找不到

在 try/catch/finally 中等待的一个很好的解决方案?