【问题标题】:WCF: Server responding with Bad Request (400)WCF:服务器响应错误请求 (400)
【发布时间】:2012-08-29 09:56:15
【问题描述】:

我的 WCF 客户端可以在 WCF 服务器上正常工作,除非在尝试传递自定义对象数组时调用服务器。当数组为 100 项时,它可以正常工作,但当项数为 3​​00 时,服务器会抛出异常 Bad Request (400)。所以我假设解决方案的关键在于配置文件。

首先是客户端的 app.config。 WCF 客户端驻留在一个 DLL 中,即 Outlook 加载项。我将此 app.config 复制到 Outlook.exe.config 到 Outlook 安装目录 - Outlook 加载加载项所需的内容:

<configuration>
  <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyProject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_IJabberSvc" closeTimeout="00:01:00"           openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:05:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:9047/switchvox/alerter" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IJabberSvc" contract="JabberService.IJabberSvc"
                name="WSDualHttpBinding_IJabberSvc">   
            </endpoint>
        </client>
    </system.serviceModel>
  <system.diagnostics>
    <trace autoflush="true" />
    <sharedListeners>
      <add name="sharedListener"
      type="System.Diagnostics.XmlWriterTraceListener"  initializeData="%AppData%\Mfg\ProjectName\Logs\SwitchvoxDialerTraceLog.svclog" />
    </sharedListeners>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Verbose, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="sharedListener"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="Verbose">
        <listeners>
          <add name="sharedListener" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

  <userSettings>
    <SwitchvoxDialer.Properties.Settings>
      <setting name="InstallSubFolder" serializeAs="String">
        <value>Mfg\\ProjectName</value>
      </setting>
      <setting name="DialerTitle" serializeAs="String">
        <value>ProjectName</value>
      </setting>
    </SwitchvoxDialer.Properties.Settings>
  </userSettings>
</configuration>

服务器配置的相关部分如下所示:

  <system.serviceModel>
    <services>
      <service name="WcfServiceLib.JabberSvc" behaviorConfiguration="JabberSvc">
        <endpoint address="http://localhost:9047/switchvox/alerter"      binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IJabberSvc" contract="WcfServiceLib.IJabberSvc" name="WSDualHttpBinding_IJabberSvc">
          <identity>

        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9047/switchvox/alerter"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="JabberSvc">
          <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483646" />
          <serviceTimeouts transactionTimeout="00:10:00" />
          <!-- 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>
    <bindings>
      <wsDualHttpBinding>
        <binding name="WSDualHttpBinding_IJabberSvc" closeTimeout="00:01:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
                 bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                 messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647"
                        maxStringContentLength="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647"/>
          <reliableSession ordered="true" inactivityTimeout="00:10:00"/>
          <security mode="Message">
            <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>
          </security>
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <!-- Enable message tracing by adding this section - Remove for production code -->
    <diagnostics>
      <messageLogging logEntireMessage="true" logMessagesAtServiceLevel="true"
      logMessagesAtTransportLevel="true"
      maxMessagesToLog="100000" />
    </diagnostics>

  </system.serviceModel>

app.config 在 bin/Debug 中生成 ServerName.exe.config(以前我将它作为嵌入式资源,但它也没有帮助......

如您所见,我将缓冲区、消息等的所有数字都增加到最大值,并确保时间值足够高......我确信问题在于服务器或客户端以某种方式不知道这些增加的值,但不知道为什么......

【问题讨论】:

  • 您是托管服务还是第三方?
  • 是我自己的自托管服务,就是我自己写的,不是IIS托管的。

标签: arrays wcf exception app-config mtom


【解决方案1】:

尝试使用 MTOM 进行消息编码。有关详细信息,请参阅以下链接:

http://msdn.microsoft.com/en-us/library/ms733742.aspxhttp://msdn.microsoft.com/en-us/library/aa395209.aspx

如果这不起作用,请使用 Fiddler 获取有关错误的更多信息。 一个 400 错误的请求可能意味着很多事情。

【讨论】:

  • 它可以与 DualHttpBinding 一起使用吗?它在这些文章中仅引用 BasicHttpBinding...
  • Fiddler 是一个非常有用的程序,谢谢...但我仍然找不到问题所在 - Fiddler 在抛出时没有显示任何异常。然后由于这个异常,客户端/服务器通道在 10-20 秒后关闭,我捕捉到客户端在关闭的通道上与服务器通信的任何其他尝试(我所看到的只是一些 HTTP 503 代码(服务不可用)但是我认为它只是反映了异常之后发生的事情......
【解决方案2】:

WCF 没有返回/显示很多关于它的实际问题的信息(悲伤)。尝试配置WCF Tracing 看看是否有帮助。

【讨论】:

  • 这是我看到的第一件事 - 它显示了相同的错误请求,没有别的......
  • 您是否尝试将调试会话附加到 IIS 并单步执行代码?
  • 没有 IIS。它是自托管的。是的,我调试它并得到这个异常,客户端调用服务器 API 试图传输 500 个项目。当我尝试 100 个项目时,它工作正常。
  • @LeonHavin:如果你能够调试它,那么抛出的异常类型是什么?堆栈跟踪指向哪里?
  • 抛出的异常是:CommunicationException: The remote server returned an unexpected response: (400) Bad Request.
猜你喜欢
  • 2012-04-27
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
相关资源
最近更新 更多