Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my project i have to pass dynamic Json string to WCF Rest service. Here both Input / Output should be in Json format. I am using server side code to pass my data. Here is my code

In testpage.aspx.cs Page
C#
string json = "{\"sjSonData\":[{"
                             + "\"log_Id\" : 0,"
                             + "\"user_Id\" : 1249,"
                             + "\"session_key\" : \"dvnoewcdw\","
                             + "\"app_id\" : 1,"
                             + "\"app_version\" :\"1.2.7\","
                             + "\"app_isOnline\" : true,"
                             + "\"app_dateTimeZone\" : \"1997-07-16T19:20:30+01:00\","
                             + "\"log_typeId\" : 1,"
                             + "\"log_description\" : \"valid\""
                         + "},"
                         + "{"
                             + "\"log_Id\" : 1,"
                             + "\"user_Id\" : 1249,"
                             + "\"session_key\" : \"dvnoewcdw\","
                             + "\"app_id\" : 1,"
                             + "\"app_version\" : \"1.2.7\","
                             + "\"app_isOnline\" : true,"
                             + "\"app_dateTimeZone\" : \"1997-07-16T19:20:30+01:00\""
                             +"}]}";

const string url = "http://localhost/RestServiceImpl.svc/jsoninputdata";

       //create an HTTP request to the URL that we need to invoke
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);                
            request.ContentType = "application/json; charset=utf-8"; //set the content type to JSON
            request.Method = "POST"; //make an HTTP POST

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                //initiate the request
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var resToWrite = serializer.Deserialize<Dictionary<string, object>>(json);
                streamWriter.Write(restoWrite);
                streamWriter.Flush();
                streamWriter.Close();
            }

            // Get the response.
            WebResponse response = request.GetResponse();                
            var streamReader = new StreamReader(response.GetResponseStream());                
            var result = streamReader.ReadToEnd();


I am new to WCF service. I don't know how to pass this data to Rest service. Here my input data has been differed every call.

Rest Service code

C#
[ServiceContract]
 public interface IRestServiceImpl
 {
   [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "jsoninputdata")]
    string jsoninputdata(List<Dictionary<string, object>> rData);
 }    

 public class RestServiceImpl : IRestServiceImpl
 {    
      public jSonResponseData jsoninputdata(List<Dictionary<string, object>> rData)
      {       
          string json = JsonConvert.SerializeObject(rData);
          //Do my stuff here.....    
          return ojSonRes; // Here i need to return my output mentioned below
      }
  }


And i need to return data (output) like below,

Condition 1: If both saved in db without any error,

{"sjSonData":[{ "success":true }]}

Condition 2: Suppose any one data failed to save in database then

{"sjSonData":[ { "log_Id":1, "success":false }, { "log_Id":2, "success":true } ]}

Now I am getting result like,

{"sjSonData":"{}"}

Am i doing anything wrong!!!!! and also please explain what are all changes i have to do in web.config file.
Posted
Updated 3-May-17 4:10am

1 solution

Hi,

I found solution for this problem. Please refer the following link DotNetCodeForU

Thanks,
D.Rajasekaran
 
Share this answer
 
Comments
Jain Nishant 13-Dec-13 4:29am    
I m facing the same problem, but the link you provided does not solve the error...
Vincent Maverick Durano 22-Sep-16 10:56am    
create a new thread and highlight the issue that you're getting. Don't forget to give the details of the issue, provide the relevant code and what you've tried so far.

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