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);
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);
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.");
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With