none
WCF HTTP Post returns 400 RRS feed

  • Question

  • Hi All,

    Im creating Wcf service and here is my IService function and config

     [OperationContract]
            [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml,
                BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml,
                UriTemplate = "DoWork")]
            string DoWork(string name);


    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
      </system.web>
      <system.serviceModel>  
        <bindings>
          <webHttpBinding>
            <binding maxReceivedMessageSize="262144"></binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false" />
            </behavior>
              <behavior name="MetadataBehavior">
                <serviceMetadata httpGetEnabled="true" />
              </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="RestBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>     
        </behaviors>    
        <protocolMapping>
          <add binding="webHttpBinding" scheme="https" />
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
        <services>
         
          <service behaviorConfiguration="MetadataBehavior" name="XXXXX.AdminOptions">
            <endpoint address="xxxxxx/AdminOptions.svc" binding="basicHttpBinding" bindingConfiguration="" name="Soap" contract="XXXX.IAdminOptions" />
            <endpoint behaviorConfiguration="RestBehavior" binding="webHttpBinding" bindingConfiguration="" name="webHttpBinding" contract="BigHartsService.IAdminOptions" />
          </service>
        </services>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true" />
      </system.webServer>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
     
    <connectionStrings><add name="db" connectionString="metadata=res://*/BigHartModel.csdl|res://*/BigHartModel.ssdl|res://*/BigHartModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=source;initial catalog=dbct;user id=XXXXX;password=XXXXX;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings></configuration>

    Here is my client code

                var client = new HttpWebRequest();
                var postData = new List<KeyValuePair<string, string>>();
                postData.Add(new KeyValuePair<string, string>("name", "Saman"));
    
                HttpContent content = new FormUrlEncodedContent(postData);
    
                var response = await client.PostAsync("http://localhost:3395/AdminOptions.svc/DoWork", content);

    i Tried clint side code with WebRequest also.. But eroor is status code returns 400..

    whats wrong and how to fix


    • Edited by PrabathM Tuesday, April 1, 2014 8:27 AM
    • Moved by Fred Bao Wednesday, April 2, 2014 7:09 AM WCF Related
    Tuesday, April 1, 2014 8:14 AM

Answers

  • Hi,

    Bad request 400 means that the request had bad syntax or was inherently impossible to be satisfied.

    Please first try to enable the wcf trace to see if it helps:

    Tracing is not enabled by default. You can enable and configure tracing by editing the application’s configuration file. The following .config example enables WCF tracing with raw message logging:

    <configuration>
      <system.serviceModel>
        <diagnostics>
          <messageLogging maxMessagesToLog="30000"
                  logEntireMessage="true"
                  logMessagesAtServiceLevel="true"
                  logMalformedMessages="true"
                  logMessagesAtTransportLevel="true">
          </messageLogging>
        </diagnostics>
      </system.serviceModel>
      <system.diagnostics>
        <sources>
          <source name="System.IdentityModel" 
                  switchValue="Verbose" 
                  logKnownPii="true">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <!-- Log all messages in the 'Messages' tab of SvcTraceViewer. -->
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <!-- ActivityTracing and propogateActivity are used to 
               flesh out the 'Activities' tab in SvcTraceViewer to 
               aid debugging. -->
          <source name="System.ServiceModel" 
                  switchValue="Error, ActivityTracing" 
                  propagateActivity="true">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <!-- This records Microsoft.IdentityModel generated traces, 
               including exceptions thrown from the framework. -->
          <source name="Microsoft.IdentityModel" switchValue="Warning">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
        </sources>
        <sharedListeners>
          <add name="xml" 
               type="System.Diagnostics.XmlWriterTraceListener" 
               initializeData="C:\logs\trace.svclog" />
        </sharedListeners>
        <trace autoflush="true" />
      </system.diagnostics>
    </configuration>

    You can read more about WCF Tracing from MSDN: Configuring Tracing.

    Microsoft provides a Service Trace Viewer Tool to read .svclog files.

    Also please try to use the fiddler to get your request and response:

    #Fiddler:
    http://www.telerik.com/fiddler .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 2, 2014 7:40 AM
    Moderator

All replies

  • Hello,

    Since this issue is more regarding WCF, I will move it to the WCF forum, there are WCF experts who will help you better.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 2, 2014 7:07 AM
  • Hi,

    Bad request 400 means that the request had bad syntax or was inherently impossible to be satisfied.

    Please first try to enable the wcf trace to see if it helps:

    Tracing is not enabled by default. You can enable and configure tracing by editing the application’s configuration file. The following .config example enables WCF tracing with raw message logging:

    <configuration>
      <system.serviceModel>
        <diagnostics>
          <messageLogging maxMessagesToLog="30000"
                  logEntireMessage="true"
                  logMessagesAtServiceLevel="true"
                  logMalformedMessages="true"
                  logMessagesAtTransportLevel="true">
          </messageLogging>
        </diagnostics>
      </system.serviceModel>
      <system.diagnostics>
        <sources>
          <source name="System.IdentityModel" 
                  switchValue="Verbose" 
                  logKnownPii="true">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <!-- Log all messages in the 'Messages' tab of SvcTraceViewer. -->
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <!-- ActivityTracing and propogateActivity are used to 
               flesh out the 'Activities' tab in SvcTraceViewer to 
               aid debugging. -->
          <source name="System.ServiceModel" 
                  switchValue="Error, ActivityTracing" 
                  propagateActivity="true">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <!-- This records Microsoft.IdentityModel generated traces, 
               including exceptions thrown from the framework. -->
          <source name="Microsoft.IdentityModel" switchValue="Warning">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
        </sources>
        <sharedListeners>
          <add name="xml" 
               type="System.Diagnostics.XmlWriterTraceListener" 
               initializeData="C:\logs\trace.svclog" />
        </sharedListeners>
        <trace autoflush="true" />
      </system.diagnostics>
    </configuration>

    You can read more about WCF Tracing from MSDN: Configuring Tracing.

    Microsoft provides a Service Trace Viewer Tool to read .svclog files.

    Also please try to use the fiddler to get your request and response:

    #Fiddler:
    http://www.telerik.com/fiddler .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 2, 2014 7:40 AM
    Moderator
  • Nothing in the seance
    Thursday, April 3, 2014 5:55 AM