Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
public class DeviceData
  {
      public string Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }


C#
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
C#
[OperationContract]
       [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate = "SaveDeviceDetails")]
       bool SaveDeviceDetails(DeviceData data);




BLEService:IBLEService (Interface Implementation)

C#
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

C#
[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.
Posted
Updated 26-Sep-14 9:46am
v2
Comments
Sergey Alexandrovich Kryukov 26-Sep-14 18:32pm    
I'm just curious: if you are already using .NET on client side, why JSON? But I would understand that this is done for serving non-.NET clients.
—SA

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.
 
Share this answer
 
v2
The idea is to make the server compatible for both .Net and non-.NET clients.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900