开发者

POST Multiple Parameters to WCF Service

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, w开发者_如何学Gohen I POST data, will it only accept a single serialized parameter?

Thank you!


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜