I’ve just spent a couple of hours debugging and looking through questions like POSTing JSON to WCF REST Endpoint and Generic WCF JSON Deserialization, but currently I think my code and/or debugging is failing at quite a basic level…
I’ve set up a WCF service like:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
public class AutomationService : IAutomationService
{
[WebInvoke(Method = "POST", UriTemplate = "getNextCommand")]
public CommandBase GetNextCommand(int timeoutInMilliseconds)
{
// stuff
}
}
where IAutomationService is:
[ServiceContract]
public interface IAutomationService
{
[OperationContract]
[ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))]
CommandBase GetNextCommand(int timeoutInMilliseconds);
}
and I’ve now got this service successfully setup with SOAP and JSON endpoints.
However… I can’t seem to work out how to call the service using variables passed in the ContentBody from Fiddler.
For example, I can call the service with a POST on the Uri – e.g.
POST http://localhost:8085/phoneAutomation/jsonAutomate/getNextCommand?timeoutInMilliseconds=10000
However, if I try to put the content in the body, then I get an exception. e.g.
POST http://localhost:8085/phoneAutomation/jsonAutomate/getNextCommand
Host: localhost:8085
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1
Accept: application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: Orchrd-=%7B%22Exp-N42-Settings%22%3A%22open%22%2C%22Exp-N42-New%22%3A%22open%22%7D
Content-Length: 31
Content-Type: application/json
{"timeoutInMilliseconds":10000}
fails with:
The server encountered an error processing the request. The exception
message is ‘There was an error deserializing the object of type
System.Int32. The value ” cannot be parsed as the type ‘Int32′.’. See
server logs for more details. The exception stack trace is:at
System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator
reader, Boolean verifyObjectName, DataContractResolver
dataContractResolver) at
System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader
reader, Boolean verifyObjectName) at…
Anyone got any ideas what I’m doing wrong (other than using WCF!) – I’m just not sure what shape the JSON {“timeoutInMilliseconds”:10000} is supposed to be.
By default the “body style” of a WCF REST service is “Bare”, meaning that for operations with a single input, the value of the operation should go “as is” without any object wrapping it. That means in your case that this will work:
One more thing, not directly related to your question: if you define a service contract in the interface, you should also add any “contract-related” attributes (such as
WebInvoke) in the interface as well. That would make your code look like this:And another info: if you wanted to send the request the same way as you had originally (
{"tiemoutInMilliseconds":10000}), you can set theBodyStyleproperty in the[WebInvoke]attribute to Wrapped (or WrappedRequest):