Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST Multiple Parameters to WCF Service

Tags:

c#

wcf

I'm trying to understand WCF, so my questions may be dumb. I believe I have a firm understanding of "GET" operations. I'm now working on some "POST" operations. My question is, can I write a WCF Service operation, with WebInvoke, that accepts multiple parameters? Or, when I POST data, will it only accept a single serialized parameter?

Thank you!

like image 812
user208662 Avatar asked Feb 06 '11 14:02

user208662


2 Answers

Yes, but your POST will have to be passed in using a common understanding of the data, aka a "data contract".

In WCF, the typical approach here is that you'd create a contract class (just an off-my-head example, not 100% working))

[DataContract(Namespace="http://yournamespace.com")]
public class MyContract
{
   [DataMember(Order=1)]
   public string MyData1 { get(); set{};}

   [DataMember(order=2)]
   public string MyData2 { get(); set{};}

}

Then you'd specify your WCF operation to accept that contract type as its parameter

[WebInvoke(method="POST")]
public string DoSomethingFromPost(MyContract postedData)
{
}

On your client, you'd serialize the data to an xml/json that matches your contract. Again, loose example:

<MyContract xmlns="http://yournamespace.com">
<MyData1>value</MyData1>
<MyData2>value</MyData2>
</MyContract>

When the contract matches, WCF will deserialze your POST into your contract object, at which point you can use it like any other class.

like image 130
Taylor Bird Avatar answered Oct 15 '22 10:10

Taylor Bird


It seems like there is a bit of confusion between wcf (which is the name given to microsofts overall abstraction for network programming) and a specific protocol HTTP, that defines verbs like "POST" and "GET", that wcf will be using to communicate.

When you define a wcf service operation and attribute it with [WebInvoke] you are going to access to the service using REST over HTTP. See webinvoke for more detail, however the remarks sum it up well

The WebInvokeAttribute attribute is applied to a service operation in addition to the OperationContractAttribute and associates the operation with a UriTemplate as well as an underlying transport verb that represents an invocation (for example, HTTP POST, PUT, or DELETE). The WebInvokeAttribute attribute is a passive operation behavior (the IOperationBehavior methods do nothing) that adds metadata to the operation description. Applying the WebInvokeAttribute attribute to a service operation has no effect unless a behavior that looks for this metadata in the operation description (such as WebHttpBehavior) is added to the service's behavior collection. The WebInvokeAttribute determines what HTTP method that a service operation responds to. By default, all methods that have the WebInvokeAttribute applied respond to POST requests.

Also further down the article defines how to map values to your service contract. Something like..

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Mod?x={x}&y={y}")]
long Mod(long x, long y);

EDIT: To make this a bit more informative for people new to the field.

  • an intro into REST without going into platform specifics.
  • and a how to set up a simple REST style wcf service using wcf
like image 43
almog.ori Avatar answered Oct 15 '22 10:10

almog.ori