iSahilSharma iSahilSharma - 18 days ago 15
C# Question

Passing a class object as input parameter to a RESTful WCF service with POST request

I have created a RESTful WCF service and trying to pass a class as an parameter to a POST request using Fiddler but facing errors like:
"HTTP/1.1 400 Bad Request"

The Interface - IXmlService.cs

<code>

[ServiceContract()]
public interface IXmlService
{
[OperationContract(Name = "Read")]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Read", BodyStyle = WebMessageBodyStyle.Wrapped)]
bool ReadData(Order data);

[OperationContract(Name = "Generate")]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Generate/")]
bool GenerateXml();
}
</code>


The Implementation - XmlService.cs

<code>

public class XmlService : IXmlService
{
public bool ReadData(Order data)
{
bool result = false;
var path = "@C:\\order.xml";
XmlSerializer serializer;
TextWriter writer;

try
{
if (data != null)
{
//serializer = new XmlSerializer(typeof(Order), new XmlRootAttribute("HEADER"));
serializer = new XmlSerializer(typeof(Order)); //No need to provide XmlRootAttribute as I have defined it on Order Model.
writer = new StreamWriter(path);
serializer.Serialize(writer, data);
result = true;
}
}
catch (Exception)
{
throw;
}
return result;
}

public bool GenerateXml()
{
throw new NotImplementedException();
}
}

</code>


The Data Model - Order.cs

<code>

[XmlRootAttribute("OrderDetails", Namespace = "http://www.ProofOfConcept.com", IsNullable = false)]
[DataContract]
public class Order
{
// The XmlArrayAttribute changes the XML element name
// from the default of "OrderedItems" to "Items".
[XmlElement("OrderId")]
[Key]
[DataMember]
public int OrderId { get; set; }

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

// Setting the IsNullable property to false instructs the
// XmlSerializer that the XML attribute will not appear if
// the City field is set to a null reference.
[XmlElementAttribute(IsNullable = false)]
[DataMember]
public string Info { get; set; }

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

//[DataMember]
//public DateTime CreatedOn { get; set; }
}

</code>


Web.config

<code>

<system.serviceModel>
<services>
<service name="WCF_XML_Service.XmlService" behaviorConfiguration="ServiceBehavior">
<!-- Service Endpoints -->
<host>
<baseAddresses>
<add baseAddress="http://localhost:16999"/>
</baseAddresses>
</host>

<endpoint address="/xml/" binding="webHttpBinding" contract="WCF_XML_Service.IXmlService" behaviorConfiguration="Web"/>
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- 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>
</serviceBehaviors>

<endpointBehaviors>
<behavior name="Web">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>

</code>


I tried different approaches by converting binding to 'webHttpBinding' in Web.Config. Also, I tried adding ' BodyStyle = WebMessageBodyStyle.Wrapped' into the WebInvoke attribute but still not able to hit the service using fiddler.

Fiddler Request:

<code>
Url - http://localhost:16999/XmlService.svc/xml/Read
Method - POST
Request Header:
User-Agent: Fiddler
Host: localhost:16999
Content-Type: text/xml
Content-Length: 155

Request Body:
{
"OrderId": "1",
"Owner": "Sam Shipping",
"Info": "First delivery shipment",
"Recipient": "Singapore Shipping Corporation"
}
</code>

Answer

Well, after wasting three days I finally figured out the error which is "Content Type" : I was using "Content-Type: text/xml" for JSON data which has to be "Content-Type: text/json".


Guess i need to clean my spectacles on daily basis for now on :)