wcf 服务远程服务器返回错误 (400) 错误的请求

wcf service the remote server returned an error (400) bad request

我有一个调用 WCF 服务的 windows 应用程序。我在某些客户端收到 400 Bad Request 错误。(有些客户端我没有收到错误。)我增加了 maxReceivedMessageSize 但它没有用。

我增加了 maxReceivedMessageSize 和其他参数。

客户端app.config:

<bindings>
  <wsHttpBinding>
    <binding name="BasicHttpBinding_IAracRandevuWS"
     closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
      <readerQuotas maxDepth="2000000" maxStringContentLength="2000000" maxArrayLength="2000000"
      maxBytesPerRead="2000000" maxNameTableCharCount="2000000" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
    <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
        algorithmSuite="Default" establishSecurityContext="true" />
      </security>

    </binding>
  </wsHttpBinding>
</bindings>

WCF 服务app.config:

<system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="BasicHttpBinding_IAracRandevuWS"
      openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
      allowCookies="false" bypassProxyOnLocal="false"  maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
      messageEncoding="Text" textEncoding="utf-8"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </wsHttpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
     <serviceDebug includeExceptionDetailInFaults="true"/>
        <serviceSecurityAudit auditLogLocation="Application" serviceAuthorizationAuditLevel="Failure" messageAuthenticationAuditLevel="Failure" suppressAuditFailure="true" />
  </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="wsHttpBinding" scheme="http"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

客户端如何生成配置文件?您使用哪种方式调用服务,添加服务引用或ChannelFactory?
wshttpbinding配置在wshttpbinding中没有很好的应用,因为我们使用协议映射来承载服务而不使用Bindingconfiguration 属性.

<protocolMapping>
  <add binding="wsHttpBinding" scheme="http"/>
</protocolMapping>

因此默认Clientcredentialtype为windows凭证,安全模式默认为message
https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/security-of-wshttpbinding
我们需要在客户端明确提供 windows 凭据。

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
            client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";

为了对此进行测试,请不要使用 Fiddler,并在控制台应用程序中使用添加服务引用从另一台计算机调用它。
如果问题仍然存在,请随时告诉我。

这是一个网络问题。更新开关后,问题已解决