将Json传递给宁静的WCF

时间:2014-10-06 10:56:23

标签: .net json web-services wcf rest

我将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)的值,但联系人的数据(键,值)虽然出现在输入中,但仍为空。知道为什么它会变空吗?如果我用soap / xml测试它,我可以看到所有输入数据,它可以正常工作。

1 个答案:

答案 0 :(得分:0)

您如何称呼您的服务? 我尝试在本地复制该问题,并且我能够通过以下方式获取一些调用服务的数据:

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);