Question

I have a WCF Rest service with a method that takes in multiple parameters:

[OperationContract]        
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json)]
StatusMessage DoSomeWork(short myId, decimal latitude, decimal longitude, string someData);

The JSON data which is serialized on the client side serializes correctly, and this method receives that data. The problem is that the longitudeparameter always comes back as 0, no matter what value the value is when it gets serialized. All the other parameters are deserialized correctly. Any idea as to why this is happening, and a possible fix?

Was it helpful?

Solution

Make sure that the name of the property in the JSON object matches the name of the parameter longitude. The code below shows your exact operation contract, and the longitude is correctly received by the service.

public class StackOverflow_23529686
{
    [ServiceContract]
    public class Service
    {
        [OperationContract]
        [WebInvoke(
            Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest, 
            RequestFormat = WebMessageFormat.Json)]
        public StatusMessage DoSomeWork(short myId, decimal latitude, decimal longitude, string someData)
        {
            return new StatusMessage
            {
                Text = string.Format("id={0}, lat={1}, lon={2}, data={3}", myId, latitude, longitude, someData)
            };
        }
    }
    public class StatusMessage
    {
        public string Text { get; set; }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        c.Headers.Add(HttpRequestHeader.ContentType, "application/json");
        var json =
            "{'someData':'hello','longitude':-56.78,'latitude':12.34,'myId':1}"
            .Replace('\'', '\"');
        Console.WriteLine(c.UploadString(baseAddress + "/DoSomeWork", json));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top