Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF is giving an error of 400 Bad Request in Post Method
    primarykey
    data
    text
    <p>I have this WCF Service:</p> <p><code>IService.cs</code>:</p> <pre><code>public interface IService { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/PostComments", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string PostComments(PostComment comment); [DataContract] public class PostComment { private string Id; private string FullName; private string Email; private string Location; private string Comments; private string Type; [DataMember] public string id { get { return Id; } set { Id = value; } } [DataMember] public string fullname { get { return FullName; } set { FullName = value; } } [DataMember] public string email { get { return Email; } set { Email = value; } } [DataMember] public string location { get { return Location; } set { Location = value; } } [DataMember] public string comments { get { return Comments; } set { Comments = value; } } [DataMember] public string type { get { return Type; } set { Type = value; } } } </code></pre> <p><code>Service.svc.cs</code>:</p> <pre><code>public string PostComments(PostComment commnt) { int ItemId; string Comments, FullName, Location, Email, Type; ItemId = Convert.ToInt32(commnt.id); Type = commnt.type; Comments = commnt.comments; FullName = commnt.fullname; Location = commnt.location; Email = commnt.email; int i = 0; if (Type == "Style") { adp = new SqlDataAdapter("insert into tblComment(intId,strComments,strFullName,strLocation,strEmail,dtPosted,blnApprove) values("+ItemId+",'"+Comments+"','"+FullName+"','"+Location+"','"+Email+"',GetDate(),1)", offcon); adp.Fill(ds1,"StComment"); DataTable dt = ds1.Tables["StComment"]; i++; } else if (Type == "Article") { adp = new SqlDataAdapter("insert into tblNewsComment(intArticleId,strComments,strFullName,strLocation,strEmail,dtPosted,blnApprove) values(" + ItemId + ",'" + Comments + "','" + FullName + "','" + Location + "','" + Email + "',GetDate(),1)", offcon); adp.Fill(ds1, "ArtComment"); DataTable dt = ds1.Tables["ArtComment"]; i++; } if (i &gt; 0) { return "Comment Successfully Submitted."; } else { return "Comment falied to Submit."; } } </code></pre> <p><code>web.config</code> file:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;connectionStrings&gt; &lt;add name="con" connectionString="Data Source=.;Initial Catalog=PatrikaData;Integrated Security=SSPI;"/&gt; &lt;/connectionStrings&gt; &lt;system.web&gt; &lt;compilation debug="true" targetFramework="4.0" /&gt; &lt;httpRuntime requestPathInvalidCharacters="&amp;lt;,&amp;gt;,*,%,:,\"/&gt; &lt;/system.web&gt; &lt;system.serviceModel&gt; &lt;services&gt; &lt;service behaviorConfiguration="ServiceBehaviour" name="WcfService.Service"&gt; &lt;endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService.IService" /&gt; &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="ServiceBehaviour"&gt; &lt;serviceMetadata httpGetEnabled="true"/&gt; &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="web"&gt; &lt;webHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment/&gt; &lt;bindings&gt; &lt;webHttpBinding&gt; &lt;binding name="web" maxBufferPoolSize="1500000" maxReceivedMessageSize="1500000" maxBufferSize="1500000"&gt; &lt;readerQuotas maxArrayLength="656000" maxBytesPerRead="656000" maxDepth="32" maxNameTableCharCount="656000" maxStringContentLength="656000" /&gt; &lt;/binding&gt; &lt;/webHttpBinding&gt; &lt;/bindings&gt; &lt;/system.serviceModel&gt; &lt;/configuration&gt; </code></pre> <p>Now if I am working with <code>BasicHttpBinding</code> and testing with the <strong>WCF test client</strong> then everything is working fine.</p> <p>But when I work with <code>WebHttpBinding</code> and testing with <strong>Google's Advanced Rest Client</strong> and posting data through that then I am getting <strong>this error</strong>:</p> <blockquote> <p>The server encountered an error processing the request. The exception message is 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'PostComments'. Encountered unexpected character 'T'.'. See server logs for more details. The exception stack trace is:</p> <p>at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p> </blockquote> <p>And Sometime this:</p> <blockquote> <p>The server encountered an error processing the request. The exception message is 'Error in deserializing body of request message for operation 'PostComments'. OperationFormatter encountered an invalid Message body. Expected to find an attribute with name 'type' and value 'object'. Found value 'number'.'. See server logs for more details. The exception stack trace is:</p> <p>at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p> </blockquote> <p>I really don't know where I am wrong as this service was working fine yesterday on the same rest client of google. I have to deploy this service on the server ASAP. Please Help!!!!</p> <p><strong>[UPDATE]</strong></p> <p>Fiddler's Output:</p> <p>HTTP/1.1 400 Bad Request</p> <pre><code>Server: ASP.NET Development Server/10.0.0.0 Date: Thu, 27 Sep 2012 10:33:24 GMT X-AspNet-Version: 4.0.30319 Content-Length: 3513 Cache-Control: private Content-Type: text/html Connection: Close </code></pre> <p>Raw Body which Google is sending:</p> <pre><code>{ "Id": "818744", "FullName": "Abhishek", "Email": "ab@gma.com", "Location": "Jaipur", "Comments": "asdkjfjk sdnfjlksdjlk dfljkfsd", "Type": "Style" } </code></pre> <p><strong>[UPDATE 2]</strong> I have gone through Service Tracker then i found that the root value which i am getting having Number and the service is expecting Object. so if it will trigger something as i am totally blank now...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload