views:

583

answers:

5

I have an operation contract (below) that I want to allow GET and POST requests against. How can I tell WCF to accept both types of requests for a single OperationContract?

[OperationContract,
WebInvoke(Method="POST",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "query")]
XElement Query(string qry);

[OperationContract,
WebInvoke(Method="GET",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "query?query={qry}")]
XElement Query(string qry);
A: 

Not using WebInvoke will do the trick.

That may not be the answer you're looking for, though.

Ian P
What do you mean not using webinvoke will do the trick?
spoon16
A: 

This post over on the MSDN Forums by Carlos Figueira has a solution. I'll go with this for now but if anyone else has any cleaner solutions let me know.

[OperationContract,
WebInvoke(Method="POST",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "query")]
XElement Query_Post(string qry);

[OperationContract,
WebInvoke(Method="GET",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "query?query={qry}")]
XElement Query_Get(string qry);
spoon16
A: 

You may want to take a look at the WebGetAttribute, I have not tried it myself but you may be able to apply it to the same method along with the WebInvokeAttribute.

Info on MSDN, and Jeff Barnes.

David
There is no difference between WebInvoke(Method = "GET") and WebGet()
spoon16
A: 

For the issue described above, changing the WebInvoke to WebGet in the case of Query_Get API will solve the issue.

Prashanth
There is no difference between WebInvoke(Method = "GET") and WebGet()
spoon16
A: 

GET and POST imply different actions though.

Won't this be confusing for clients, and wrong from a REST perspective?

I'm coding the endpoint based on a specification from the W3C. That specification describes one method that supports multiple operations (GET and POST). If the query is to large for a GET request (URL length) the consumer can use POST.
spoon16