簡體   English   中英

錯誤 - 遠程服務器返回意外響應:(400)錯誤請求。

[英]Error-The remote server returned an unexpected response: (400) Bad Request.

我有一個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;
        }
    }

4xx開頭的錯誤代碼(即四百個)意味着問題在於您發送到服務器的數據 - 服務器無法理解您發送的數據。 例如,如果請求需要整數參數,但是您發送了一個字符串,則會看到此問題。

(相比之下, 5XX錯誤意味着服務器理解您的請求,但在處理過程中引發了錯誤。)

通常,WCF服務中的4xx錯誤意味着請求甚至沒有到達您的代碼,因為WCF可能無法將您發送的數據反序列化為調用方法所需的類型。 在這種情況下,如果您發布的數據不是有效的Cable ,那么在沒有調用代碼的情況下,您將看到400錯誤。

您可以通過檢查您發送的請求來測試這一點,也可以通過編寫一個小測試工具(我建議使用Linqpad !)來手動反序列化您的請求主體 - 您可以在那里找到問題的原因。

您可能需要打開WCF日志記錄來解決這個問題。 日志記錄將顯示入站請求的內容,以及您的服務對它的反應。 基於此,您應該能夠找出問題所在。

這是“如何”的鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM