.net - Passing Json to restful WCF -
.net - Passing Json to restful WCF -
i'm sending (post) json info 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 processing homecoming true; }
when utilize fiddler send input info (json), in debug mode see input (emps) contains values emp (ie fname , joindate) info contact (key, value) coming in empty though nowadays in input. thought why coming in empty? if test soap/xml, can see input info , works fine.
how calling service? tried replicate issue locally , able info calling service next 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() httpwebresponse; encoding enc = system.text.encoding.getencoding("utf-8"); // read response stream response object streamreader loresponsestream = new streamreader(webresponse.getresponsestream(), enc); // read string stream info string strresult = loresponsestream.readtoend(); // close stream object loresponsestream.close(); // close response object webresponse.close(); // assign final result text box response.write(strresult);
.net json web-services wcf rest
Comments
Post a Comment