C# 错误远程服务器返回意外响应:(400)错误请求。

C# 错误远程服务器返回意外响应:(400)错误请求。,c#,visual-studio-2010,wcf,C#,Visual Studio 2010,Wcf,我有一个WCF,我得到了 "The remote server returned an unexpected response: (400) Bad Request." 调用它的特定方法时出错 这是服务器端的web.Config <configuration> <connectionStrings> <remove name="LocalSqlServer"/> <add name="CableContext" connectionString="me

我有一个WCF,我得到了

"The remote server returned an unexpected response: (400) Bad Request."
调用它的特定方法时出错

这是服务器端的web.Config

<configuration>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="CableContext" connectionString="metadata=res://*/CableModel.csdl|res://*/CableModel.ssdl|res://*/CableModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=pgdbserver;initial catalog=CableDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />     
 </connectionStrings>

<system.web>
<compilation debug="true" targetFramework="4.0" />

</system.web>
<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- 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>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
  <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>

   </configuration>

您可能需要打开WCF日志来解决这个问题。日志记录将向您显示入站请求的内容,以及您的服务对该请求的反应。基于此,您应该能够找出问题所在


4xx
开头的错误代码(即400多个)表示问题出在您发送到服务器的数据上-服务器无法理解您发送的数据。例如,如果请求需要一个整数参数,但您发送了一个字符串,您将看到这个问题

(相比之下,
5XX
错误意味着服务器理解您的请求,但在处理过程中抛出了一个错误。)

通常,WCF服务中的
4xx
错误意味着请求甚至没有到达您的代码,因为WCF可能无法将发送的数据反序列化为调用方法所需的类型。在这种情况下,如果您发布的数据不是有效的
电缆
,您将看到
400
错误,而您的代码从未被调用


您可以通过检查发送的请求来测试这一点,也可以通过编写一个小测试工具(我建议使用!)来手动反序列化您的请求体-您可能会在那里找到问题的原因。

那么,您是否调试了您的方法?你有没有办法确保它在没有异常的情况下正常运行?我调用方法client.Export(diffCable)时会发生这种情况,我无法进入服务,这就是为什么你独立于WCF测试它。在测试项目中创建服务类的实例,并从代码中调用该方法。先看看它在没有WCF的情况下是否工作。如果是,请检查您的Cable参数是否可正确序列化。抱歉,我是这方面的新手,您能给我一个如何测试它以及如何知道它是否已正确序列化的示例吗?您应该按照上一条评论中描述的方法进行操作。如果您想要一个简单的测试,请注释掉您的函数体并改为“return0;”。如果仍然失败,请发布参数的类声明。如果它与零一起工作,则代码有错误。以正常方式查找并修复该问题。
 <?xml version="1.0" encoding="utf-8"?>
   <configuration>
     <appSettings>
       <add key="UserManager" value="http://appserver:8080/SecurityServices/UserManager.asmx" />
      <add key="ClientSettingsProvider.ServiceUri" value="" />
     </appSettings>
    <system.serviceModel>
    <bindings>
      <basicHttpBinding>
          <binding name="BasicHttpBinding_ICableService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="9830400" maxBufferPoolSize="524288" maxReceivedMessageSize="9830400" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="64" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
       </binding>
      </basicHttpBinding>
     </bindings>
     <client>      
     <endpoint address="http://appserver:8080/CableDataService/CableService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICableService" contract="CableServiceReference.ICableService" name="BasicHttpBinding_ICableService" />
    </client>
   </system.serviceModel>
   <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
  <providers>
    <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
  </providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
  <providers>
    <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
  </providers>
  </roleManager>
  </system.web>
  </configuration>
   public int Export(Cable cableToSave )
    {
        int result = 0;
        using (UnitOfWork unitOfWork = new UnitOfWork())
        {

                        if (cableToSave.CableProperty != null && cableToSave.CableProperty.CableApplication != null && cableToSave.CableProperty.CableApplication.State == State.Added)
                        {
                            cableToSave.CableProperty.CableApplication.CableProperties = null;
                            unitOfWork.CableApplicationRepository.Insert(cableToSave.CableProperty.CableApplication);
                        }
                        if (cableToSave.CableProperty != null && cableToSave.CableProperty.State == State.Added)
                        {
                            cableToSave.CableProperty.Cables = null;
                            unitOfWork.CablePropertyRepository.Insert(cableToSave.CableProperty);
                        }
                        if (cableToSave.State == State.Added)
                        {
                            unitOfWork.CableRepository.Insert(cableToSave);
                            result = cableToSave.Id;
                            if (cableToSave.Cores != null)
                            foreach (Core coreToSave in cableToSave.Cores)
                            {
                                unitOfWork.CoreRepository.Insert(coreToSave);
                            }
                        }                            


            unitOfWork.Save();
            return result;
        }
    }