我正在做一个需要通过客户端接收复杂数据的WCF REST服务。我知道我不能通过GET方法传递复杂的数据,所以我使用POST来发送它。
我有这样的方法定义......
[WebInvoke (UriTemplate="/test/item", Method="POST",
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Wrapped)]
[OperationContract]
string TestMethod(InItem item);
我正试图用.Net控制台客户端这样消费......
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myIP:port/test/item");
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
InItem item = new InItem()
{
Quantity = 2,
BougthQuantity = 2,
Code = "9998",
Date = DataTime.Now
};
string itemToSend = JsonConvert.SerializeObject(item);
byte[] byteData = Encoding.UTF8.GetBytes(itemToSend);
request.ContentLength = byteData.Length;
request.ContentType = "application/json;";
Stream stream = request.GetRequestStream();
stream.Write(byteData, 0, byteData.Length);
stream.Close();
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
但是当我调用服务方法时,TestMethod上的'item'参数总是为null,我错过了什么或者我做错了什么?
提前致谢...