Read request content in JSON form from OperationContext in C#

1.9k views Asked by At

I have created WCF RESTful service as below:

[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Customer/{customerID}/profile")]
string PutCustomerProfileData(string customerID);

I'm debugging this using Postman and passing JSON data in BODY as below:

{ "customerID":"RC0064211", "TermsAgreed":"true" }

public string PutCustomerProfileData(string customerID)
{
    Message requestMessage = OperationContext.Current.RequestContext.RequestMessage;
}

What it returns in RequestMessage is as below:

{<root type="object">
  <customerID type="string">RC0064211</customerID>
  <TermsAgreed type="string">true</TermsAgreed>
</root>}

I want this request body in JSON form. Can I have it? If not what is the other way that I can create JSON string for mentioned RequestMessage?

3

There are 3 answers

0
I Love Stackoverflow On BEST ANSWER

I tried with the DataContract and DataMember and it worked for me.

Below is the sample code:

[OperationContract]
    [WebInvoke(Method = "PUT",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/Customer/{customerID}/verification")]
    string PutCustomerVerificationData(string customerID, CustomerVerification customerVerification);
}

[DataContract]
public class CustomerVerification
{
    [DataMember]
    public string PasswordHash { get; set; }

    [DataMember]
    public string PasswordSalt { get; set; }
}

Then I have converted that DataContract to the JSON string and used it further as below:

public string PutCustomerVerificationData(string customerID, CustomerVerification customerVerification)
{
      JavaScriptSerializer js = new JavaScriptSerializer();
      string requestBody = js.Serialize(customerVerification);
      string serviceResponse = bllCustomerDetails.PutCustomerVerificationData(customerID, requestBody).Replace("\"", "'");
      return serviceResponse;
}
0
Kont Dooku On

I actually did not fully understand the problem but as a recommendation I might suggest;

You should design your WebConfig for Json like this;

    <services>
  <service name="Your Service Name"

    <endpoint address="" behaviorConfiguration="webHttp" binding="webHttpBinding"
              bindingConfiguration="webHttpBindingWithJsonP" contract="YourProjectName">

    </endpoint>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJsonP" />

             </binding>
  </webHttpBinding>
</bindings>


<behaviors>
  <endpointBehaviors>
    <behavior name="webHttp">
      <webHttp />
    </behavior>
  </endpointBehaviors>

And your data member should like this(just example);

   [DataContract]
public class Customer
{
   [DataMember]
    public int ID { get; set; }

    [DataMember]
    public int customerID { get; set; }

}

Also, you can try on your webservice on Fiddler 4 and you can request and response as JSON or what you want.

3
Rohan Kadu On

Add [DataMember] over the member variables which you want to convert into JSON.