C# – WCF Restful post Services returns BAD REQUEST(400)


I am trying from few days to consume wcf restful service but it is giving me bad reqeust. Please Help me out.

Here is my configuration file


    <binding name="state" allowCookies="true">
      <security mode="None"></security>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    <behavior name="web">
  <service behaviorConfiguration="ServiceBehaviour" name="RESTFUL_DEMO.Web.services.Calc">
    <endpoint address="" bindingConfiguration="state" binding="webHttpBinding" name="Http" contract="RESTFUL_DEMO.Web.services.ICalc"/>
    <endpoint address="abcd" binding="wsHttpBinding" name="wsHttp" contract="RESTFUL_DEMO.Web.services.ICalc"/>

    <endpoint address="mex" binding="mexHttpBinding" name="MEX" contract="IMetadataExchange"/>


my interface for service contract and datacontract is as follows.

[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface ICalc
    [WebInvoke(UriTemplate = "dowork", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    int DoWork(Enroll a);

public class Enroll
    public Enroll()

    public Enroll(string Avalue)
        this.Avalue = Avalue;
    [DataMember(IsRequired = true)]
    public string Avalue


my code to consume the service is as follows

HttpWebRequest request = WebRequest.Create("http://localhost/RESTFUL_DEMO.Web/services/Calc.svc/dowork") as HttpWebRequest;
XmlDocument doc = new XmlDocument();
        string sXML = doc.InnerXml;
        request.ContentLength = sXML.Length;
        request.ContentType = "test/xml; charset=utf-8";
        var sw = new StreamWriter(request.GetRequestStream());
        WebResponse response = request.GetResponse();
        StreamReader stream = new StreamReader(response.GetResponseStream());
        String result = stream.ReadToEnd();

Best Solution

You have made a small mistake while consuming Rest service. You have specified ContentType of request to test/xml; charset=utf-8 but it should be text/xml or application/xml.

request.ContentType = "text/xml; charset=utf-8";

or it should be

request.ContentType = "application/xml";