3

Hi i am new to the RESTful WCF I am trying to do the the simple call to the webservice method using POST here is my code

Service Interface code

 [ServiceContract]
public interface IJsonSave
{

    [OperationContract]
    [WebInvoke(UriTemplate = "/SaveJason", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
        Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string SaveJason(string value);
}

web.config file code

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
   <behaviors>
     <serviceBehaviors>
       <behavior name ="servicebehavior">
          <!-- 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>
  <endpointBehaviors>
    <behavior name="restbehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<services>
  <service name ="JsonDataSave.JsonSave"
           behaviorConfiguration ="servicebehavior" >
    <endpoint name ="SOAPEndPoint"
              contract ="JsonDataSave.IJsonSave"
              binding ="basicHttpBinding"
              address ="soap" />
    <endpoint name ="RESTEndPoint"
              contract ="JsonDataSave.IJsonSave"
              binding ="webHttpBinding"
              address ="Rest"
              behaviorConfiguration ="restbehavior" />
    <endpoint contract="IMetadataExchange"
              binding="mexHttpBinding"
              address="mex" />
  </service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
 </system.webServer>

</configuration>

I tried using fiddler her are my Headers

POST http://localhost:50267/JsonSave.svc/Rest/SaveJason
User-Agent: Fiddler
Content-Type: application/json
Data-Type: json
Host: localhost:50267

Request Body :

 ({"value":"asdfasd"})

It gives error of HTTP/1.1 400 Bad Request and when debug detail enabled it gives following stack trace

The server encountered an error processing the request. The exception message is 'Error    in deserializing body of request message for operation 'SaveJason'. The OperationFormatter could not deserialize any information from the Message because the Message is empty (IsEmpty = true).'. See server logs for more details. The exception stack trace is: 
at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.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& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

I googled a lot and tried every solution there is available but it still didn't work any help would be great.And it works for the SOAP just gives error with REST!

  • Try use the `BodyStyle = WebMessageBodyStyle.Bare` instead. – Atlasmaybe Dec 02 '13 at 10:07
  • @Atlasmaybe tried that still gives the exception –  Dec 02 '13 at 10:11
  • @Atlasmaybe and if don't send anything from the requestbody then it executes correctly but if i send the value of the parameter `({"value":"sdfdas"})` then it throws exception –  Dec 02 '13 at 10:13
  • It's probably a stupid question but we never know... Do you send data length in your request ? – Atlasmaybe Dec 02 '13 at 10:16
  • @Atlasmaybe yes i send the content-Length in the headers –  Dec 02 '13 at 10:22
  • @Atlasmaybe Hey i solved it i have to set the `BodyStyle = WebMessageBodyStyle.Wrapped` and it worked thanks for the help –  Dec 02 '13 at 10:23

1 Answers1

1

In order to gather additional information about the data flow and location of the error you may benefit from enabling WCF Tracing on the service side. The following link should provide sufficient details: http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

Regarding the error in your scenario, I suspect that the message encoding is not set consistently for the client and service. I wonder if maybe the:

BodyStyle = WebMessageBodyStyle.WrappedRequest

should be either:

BodyStyle = WebMessageBodyStyle.Wrapped

or

BodyStyle = WebMessageBodyStyle.Bare

so the request and response have the same wrapping style.

Regards,

Seymour
  • 7,043
  • 12
  • 44
  • 51
  • 1
    hey thanks for help `BodyStyle = WebMessageBodyStyle.Wrapped` worked –  Dec 03 '13 at 07:27