確保JSON對象中的屬性名稱與參數longitude
的名稱匹配。下面的代碼顯示了您的確切操作合同,並且服務正確接收了經度。
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();
}
}
如何將數據發送到服務?你能舉一個你在做什麼的例子嗎? – carlosfigueira
我通過WebClient將它作爲JSON發送。我目前無法發佈示例,但它是通過WebService將字符串作爲JSON發佈的標準方式;沒有什麼奇特的事情發生。 –