1

All my GET endpoints work like a champ, but I'm trying to implement a webinvoke method="POST".

I think there is something wrong with my format, but I can't tell what it is, could someone help?

[ServiceContract]
interface iFlowRate
{
     [OperationContract]
     [WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}",RequestFormat= WebMessageFormat.Xml)]
     string AddFlowRate(string apikey,FlowRate flowrate);
}

when I debug this it doesnt even get into this method. I'm calling the service like this.

string postData = "<FlowRate ><wellname>wellname</wellname></FlowRate>";
//Setup the http request.
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentLength = postData.Length;
request.ContentType = "application/xml";
request.KeepAlive = true;

StreamWriter streamwriter = new
StreamWriter(request.GetRequestStream());
streamwriter.Write(postData);
streamwriter.Close();

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Read the response
StreamReader responsereader = new StreamReader(response.GetResponseStream());
string strResponseData = responsereader.ReadToEnd();

Any ideas? BTW, using WCF 4.0, any help is much appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Matt
  • 97
  • 8

1 Answers1

1

This was totally killing me until moments ago where I finally stumbled upon the answer.

Here is the source for my findings: Wrapped BodyStyle in WCF Rest

But I'll cut to the good stuff.

First, set the Namespace of your ServiceContract.

[ServiceContract(Namespace = "http://mytagservice")]

Now, I'm sure there is another way to get this to work but this is how I hacked it. Set the BodyStyle to Wrapped. The default request format is XML so you don't need to set it here if you don't want to.

 [WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}", BodyStyle = WebMessageBodyStyle.Wrapped)]

Then change your xml to include the wrapper and namespace. Be extra careful of the tag names as they are case sensitive.

string postData = "<AddFlowRate xmlns='http://mytagservice'><flowrate><wellname>wellname</wellname></flowrate></AddFlowRate>";

Because it's using the wrapped message type this solution will work for as many parameters as you can want.

Scott
  • 2,593
  • 1
  • 22
  • 23