Creating a WCF JSON (non RESTful) Service
I am trying to create a simple non-RESTful JSON service using WCF and .NET 4. I'd like my service to be able to parse a JSON request message with a specific format, something like this:
{ "MethodNameRequest": { "MethodParam1Name": "ParamValue1", "MethodParam2Name": "ParamValue2" } }
The endpoint for this service should reside in a single constant URI ("http://myserver/myservice/") so that all methods could be invoked using a POST request to it.
The problem is that whenever I try to declare two (or more) methods using the same "UriTemplate" and the same HTTP verb "POST" (using WebInvokeAttribute), like this:
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "")]
public string Method1()
{
return "Method1";
}
[WebInvoke(BodyStyle =开发者_开发问答 WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "")]
public string Method2()
{
return "Method2";
}
I get the following exception:
In contract '', there are multiple operations with Method 'POST' and a UriTemplate that is equivalent to ''. Each operation requires a unique combination of UriTemplate and Method to unambiguously dispatch messages. Use WebGetAttribute or WebInvokeAttribute to alter the UriTemplate and Method values of an operation.
Any ideas on how I can configure WCF to allow this?
I don't see how WCF could figure out which method to call if it somehow allowed the identical UriTemplate for the different methods. Seems you need to implement logic inside the method to handle content based processing.
Try to ommit the UriTemplate
property, use instead the <enableWebScript/>
element in web.config
. This will allow wcf
to automatically handle the requests for you.
精彩评论