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

时间:2013-11-19 06:00:11

标签: 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="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>

这是客户端的app.config

 <?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;
        }
    }

2 个答案:

答案 0 :(得分:2)

4xx开头的错误代码(即四百个)意味着问题在于您要发送到服务器的数据 - 服务器无法理解您所拥有的数据发送。例如,如果请求需要一个整数参数但是你发送了一个字符串,那么你会看到这个问题。

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

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

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

答案 1 :(得分:0)

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

Here's a link to the "how to"