简体   繁体   中英

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

I have a WCF which I get the

"The remote server returned an unexpected response: (400) Bad Request."

error just in calling a specific Method of it.

This is web.Config in server side

<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>

and this is the app.config in client side

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

and this is the code I am Calling

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

An error code beginning with 4xx (that is, four-hundred-something) means that the problem is with the data you're sending to the server - the server can't understand the data you're sending. For example, if the request expects an integer parameter but you send a string, you'll see this problem.

(By contrast, a 5XX error means that the server understood your request, but threw an error during the processing.)

Often, a 4xx error in a WCF service means that the request isn't even reaching your code, because it may be that WCF can't deserialize the data you're sending into the types required to call your methods. In this case, if you're posting data that isn't a valid Cable , you'll see a 400 error without your code ever being called.

You can test this by examining the requests that you're sending, and also by writing a small test harness (I recommend using Linqpad !) to deserialize your request body manually - you may find the cause of your issue there.

You'll probably need to turn on the WCF logging to figure this out. The logging will show you the content of the inbound request, plus your service's reaction to it. Based on that, you should be able to figure out the problem.

Here's a link to the "how to"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM