将Json传递给宁静的WCF(Passing Json to restful WCF)

编程入门 行业动态 更新时间:2024-10-25 00:32:03
将Json传递给宁静的WCF(Passing Json to restful WCF)

我正在发送(发布)Json数据到wcf服务。


public interface IRegisterEmployee { [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, UriTemplate = "AddEmployee")] bool ProcessEmployee(Employee emps); }
[DataContract] public class Employee { [DataMember] public Emp[] emps { get; set; } }
DataContract] public class Emp { [DataMember] public string FName { get; set; } [DataMember] public string joinDate {get; set; } [DataMember] public Contact[] contacts {get; set; } }
DataContract] public class Contact { [DataMember] public string key { get; set; } [DataMember] public string value {get; set; } }
public class RegisterEmployee : IRegisterEmployee { public bool ProcessEmployee(Employee emps) { //do some processing return true; }

当我使用fiddler发送输入数据(json)时,在调试模式下,我看到输入(emps)包含Emp的值(即FName和joinDate)但是Contact (key,value)的数据虽然是空的,但仍然是空的出现在输入中。 知道为什么它会变空吗? 如果我用soap / xml测试它,我可以看到所有输入数据,它工作正常。

I'm sending (post) Json data to a wcf service.


public interface IRegisterEmployee { [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, UriTemplate = "AddEmployee")] bool ProcessEmployee(Employee emps); }
[DataContract] public class Employee { [DataMember] public Emp[] emps { get; set; } }
DataContract] public class Emp { [DataMember] public string FName { get; set; } [DataMember] public string joinDate {get; set; } [DataMember] public Contact[] contacts {get; set; } }
DataContract] public class Contact { [DataMember] public string key { get; set; } [DataMember] public string value {get; set; } }
public class RegisterEmployee : IRegisterEmployee { public bool ProcessEmployee(Employee emps) { //do some processing return true; }

When I use fiddler to send input data (json), in debug mode I see that input (emps) contains values for Emp (ie FName and joinDate) however the data for Contact (key, value) are coming in as empty though it was present in the input. Any idea why it is coming in as empty? If I test this with soap/xml, i can see all input data and it works fine.

最满意答案

你怎么称呼你的服务? 我试图在本地复制问题,我能够通过以下方式获取一些调用服务的数据:

string sURL = "http://localhost:13104/RegisterEmployee.svc/AddEmployee"; var employee = new Employee { emps = new Emp[1] { new Emp { FName = "MyFName", joinDate = "12/01/2012", contacts = new WcfService1.Contact[1] { new WcfService1.Contact { key = "myKey", value="myVailue" } } } } }; var json = Newtonsoft.Json.JsonConvert.SerializeObject(employee); ASCIIEncoding encoding = new ASCIIEncoding (); byte[] byte1 = encoding.GetBytes (json); WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); wrGETURL.Method = "POST"; wrGETURL.ContentType = @"application/json; charset=utf-8"; wrGETURL.ContentLength = byte1.Length; Stream requestStream = wrGETURL.GetRequestStream(); requestStream.Write(byte1, 0, byte1.Length); requestStream.Close(); HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse; Encoding enc = System.Text.Encoding.GetEncoding("utf-8"); // read response stream from response object StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc); // read string from stream data string strResult = loResponseStream.ReadToEnd(); // close the stream object loResponseStream.Close(); // close the response object webresponse.Close(); // assign the final result to text box Response.Write(strResult);

How are you calling your service? I tried to replicate the issue locally and I was able to get some data calling the service the following way:

string sURL = "http://localhost:13104/RegisterEmployee.svc/AddEmployee"; var employee = new Employee { emps = new Emp[1] { new Emp { FName = "MyFName", joinDate = "12/01/2012", contacts = new WcfService1.Contact[1] { new WcfService1.Contact { key = "myKey", value="myVailue" } } } } }; var json = Newtonsoft.Json.JsonConvert.SerializeObject(employee); ASCIIEncoding encoding = new ASCIIEncoding (); byte[] byte1 = encoding.GetBytes (json); WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); wrGETURL.Method = "POST"; wrGETURL.ContentType = @"application/json; charset=utf-8"; wrGETURL.ContentLength = byte1.Length; Stream requestStream = wrGETURL.GetRequestStream(); requestStream.Write(byte1, 0, byte1.Length); requestStream.Close(); HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse; Encoding enc = System.Text.Encoding.GetEncoding("utf-8"); // read response stream from response object StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc); // read string from stream data string strResult = loResponseStream.ReadToEnd(); // close the stream object loResponseStream.Close(); // close the response object webresponse.Close(); // assign the final result to text box Response.Write(strResult);

更多推荐

本文发布于:2023-08-07 12:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464271.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宁静   Json   WCF   restful   Passing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!