Hi guys,
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
------------------------
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.ContentLength = byteArray.Length;
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>(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
-------------------
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "jsoninputdata")]
string jsoninputdata(List> rData);
}
public class RestServiceImpl : IRestServiceImpl
{
public jSonResponseData jsoninputdata(List> 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.
Jaganathan BantheswaranPosted Dec 10, 2013, 3:03 AM
My suggestion is,
Try to use ExpandoObject to form json string.
Try to use DataContract in Service.
I suspect the Dictionary
Follow these articles as well,
vivek anandPosted Feb 24, 2015, 1:48 AM
Raja sekaranPosted Dec 12, 2013, 4:27 AM
Thanks for your valuable reply. Your links are useful but its not rectify my problem. I always get the same issue. I tried restsharp dll file now it's working fine.
Thanks,
D.Rajasekaran
Jaganathan BantheswaranPosted Dec 12, 2013, 2:06 AM
Raja sekaranPosted Dec 12, 2013, 12:46 AM
I found solution for this problem. Please refer the following link http://dotnetcodeforu.blogspot.in/2013/12/pass-json-string-data-to-rest-service.html
Thanks,