Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable multiple HTTP Methods on a single operation?

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);
like image 536
Eric Schoonover Avatar asked Feb 16 '09 23:02

Eric Schoonover


3 Answers

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);
like image 104
Eric Schoonover Avatar answered Nov 04 '22 23:11

Eric Schoonover


Incase if anyone looking for a different solution,

[OperationContract]
[WebInvoke(Method="*")]
public <> DoWork()
{
     var method = WebOperationContext.Current.IncomingRequest.Method;
     if (method == "POST") return DoPost();
     else if (method == "GET") return DoGet();
     throw new ArgumentException("Method is not supported.");
}
like image 18
Prem Rajendran Avatar answered Nov 05 '22 00:11

Prem Rajendran


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.

like image 1
David Avatar answered Nov 05 '22 00:11

David