本文介绍了如何将数据发布到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个客户端,我试图将数据发送到JSON格式的基于Rest的WCF服务器和服务器端的数据对象获得空值。 以下是客户的代码段: public class DeviceData { public 字符串 ID { get ; set ; } public string FirstName { get ; set ; } public string LastName { get ; set ; } } var httpWebRequest =(HttpWebRequest)WebRequest.Create( http:// localhost:62570 / BLEService.svc / SaveDeviceDetails ); httpWebRequest.ContentType = application / json; httpWebRequest.Method = POST; var serializer = new Newtonsoft.Json.JsonSerializer(); 使用( var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { 使用( var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter)) { serializer.Serialize(tw, new { method = SaveDeviceDetails, @params = new DeviceData {FirstName = abc,Id = 7,LastName = def} }); } } var httpResponse =(HttpWebResponse)httpWebRequest.GetResponse(); 使用( var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var responseText = streamReader.ReadToEnd(); } 这是我的服务器实现: IBLEService.cs [OperationContract] [WebInvoke(方法) = POST,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate = SaveDeviceDetails)] bool SaveDeviceDetails(DeviceData data); BLEService:IBLEService(接口实现) bool IBLEService.SaveDeviceDetails(DeviceData data) { try { System.IO.File。 WriteAllText( C:\\Device.txt,data.FirstName + + data.LastName + + data.Id); } catch (例外情况) { throw new FaultException< string> (ex.Message); } 返回 true ; } 服务器中的DeviceData.cs [DataContract] public class DeviceData { [DataMember(Name = Id)] public string ID { get ; set ; } [DataMember(Name = FirstName)] public string FirstName { get ; set ; } [DataMember(Name = LastName)] public string LastName { get ; set ; } } 这里服务器中的数据我得到了NULL,任何帮助将不胜感激。解决方案 看起来JSON序列化器与WCF不太兼容,因此我将我的服务引用添加到客户端和我能够将我的数据发布到服务。 我们的想法是使服务器兼容.Net和非.NET客户端。 Hi,I have a client from which I am trying to POST data to a Rest based WCF server in JSON format and my data object on the server side is getting null value.Here is the code snippet for Client:public class DeviceData { public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:62570/BLEService.svc/SaveDeviceDetails"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; var serializer = new Newtonsoft.Json.JsonSerializer(); using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { using (var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter)) { serializer.Serialize(tw, new { method = "SaveDeviceDetails", @params = new DeviceData{FirstName = "abc",Id = "7", LastName = "def"} }); } } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var responseText = streamReader.ReadToEnd(); }Here is my Server Implementation:IBLEService.cs[OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate = "SaveDeviceDetails")] bool SaveDeviceDetails(DeviceData data);BLEService:IBLEService (Interface Implementation)bool IBLEService.SaveDeviceDetails(DeviceData data) { try { System.IO.File.WriteAllText("C:\\Device.txt", data.FirstName + " " + data.LastName + " " + data.Id); } catch (Exception ex) { throw new FaultException<string> (ex.Message); } return true; }DeviceData.cs in Server[DataContract] public class DeviceData { [DataMember(Name = "Id")] public string Id { get; set; } [DataMember(Name = "FirstName")] public string FirstName { get; set; } [DataMember(Name = "LastName")] public string LastName { get; set; } }Here the data in server I m getting as NULL , any help will be appreciated Thanks. 解决方案 It looks like the JSON serializers are not much compatible with WCF, therefore I added my service reference to the client and I am able to post my data to the service.The idea is to make the server compatible for both .Net and non-.NET clients. 这篇关于如何将数据发布到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 06:40