2

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.

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165

1 Answers1

1

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:

POST http://localhost:8085/phoneAutomation/jsonAutomate/getNextCommand
Host: localhost:8085
Connection: keep-alive
Cache-Control: max-age=0
...
Content-Length: 5
Content-Type: application/json

10000

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:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
public class AutomationService : IAutomationService
{
    public CommandBase GetNextCommand(int timeoutInMilliseconds)
    {
        // stuff
    }
}

[ServiceContract]
public interface IAutomationService
{
    [OperationContract]
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))]
    [WebInvoke(Method = "POST", UriTemplate = "getNextCommand")]
    CommandBase GetNextCommand(int timeoutInMilliseconds);
}

And another info: if you wanted to send the request the same way as you had originally ({"tiemoutInMilliseconds":10000}), you can set the BodyStyle property in the [WebInvoke] attribute to Wrapped (or WrappedRequest):

[ServiceContract]
public interface IAutomationService
{
    [OperationContract]
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))]
    [WebInvoke(Method = "POST",
               UriTemplate = "getNextCommand",
               BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    CommandBase GetNextCommand(int timeoutInMilliseconds);
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • 1
    I think I love you. Thank you for helping! – Stuart Oct 19 '11 at 17:56
  • Been and tested it now... everything you say works... now I know I love you. Thank you. Thank you. Thank you. Will also go off and tidy up my contract too. – Stuart Oct 19 '11 at 18:04