This is how I convert a C# object to a Json string, send it to a web service, get a Json string response from the web service, and convert the Json string back into a C# object. I’m new at this, so I may be performing unnecessary steps …but it works.

You can get detailed instructions for creating the WCF web service that I use in this example. CLICK HERE FOR DETAILS.

In Visual Studio, within the Solution Explorer area: right click “Solution ‘MyNamespace'” -> Add -> New Project… -> Visual C# -> Console Application. Then enter ‘ConsoleApplication1’ in the ‘Name:’ textbox and click the ‘OK’ button.

In the Solution Explorer area: right click ConsoleApplication1 -> Set as StartUp Project.

In the Solution Explorer area: right click ConsoleApplication1 -> Add -> New Item… -> Visual C# Items -> Class. Then enter ‘CompositeType.cs’ in the ‘Name:’ textbox and click the ‘OK’ button.

Open ‘CompositeType.cs’ and replace ‘class CompositeType{}’ with the following code:

public class CompositeType {

bool boolValue = true;
string stringValue = “Hello “;

public bool BoolValue{

get { return boolValue; }
set { boolValue = value; }

}

public string StringValue{

get { return stringValue; }
set { stringValue = value; }

}

}

Add this to the ‘IMyService.cs’ file in the ‘MyNamespace’ project, then save the changes:

[OperationContract]
[WebInvoke(Method = “POST”,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = “ReceiveJsonStringAndReturnIt”)]
CompositeType ReceiveJsonStringAndReturnIt(CompositeType param);

Add this to the ‘MyService.svc.cs’ file in the ‘MyNamespace’ project, then save the changes:

public CompositeType ReceiveJsonStringAndReturnIt(CompositeType param) {

//i am just returning the input parameter, unmodified.
//you should probably do something meaningful here.
return param;

}

In the Solution Explorer area: ConsoleApplicaiton1 -> right click ‘References’ -> Add Reference… -> find/select ‘System.Web.Extensions’ -> click ‘OK’ button

In the Solution Explorer, open ‘Program.cs’. Add the following code in the first line of the file:

using System.Web.Script.Serialization; //this comes from System.Web.Extensions
using System.Net;
using System.IO;

Then add the following code within the ‘static void Main’ method: (Be sure to replace ‘http://localhost:55761 ‘ with the correct value. CLICK HERE FOR DETAILS)

CompositeType myCompositeType = new CompositeType() { StringValue=”here is my string value”, BoolValue=true }; //create c# CompositeType object
var serializedObject = new JavaScriptSerializer().Serialize(myCompositeType); //get Json string representation of object
//Console.WriteLine(serializedObject); //uncomment this in order to see what the Json string looks like

byte[] byteArray = Encoding.UTF8.GetBytes(serializedObject); //create a byte array for the datastream
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“http://localhost:55761/MyService.svc/ReceiveJsonStringAndReturnIt “); //create a request to send to the web service
request.Method = “POST”; //use the POST method (as opposed to GET)
request.ContentType = “application/json”; //set the content type to Json
Stream dataStream = request.GetRequestStream(); //establish a stream to the web service
dataStream.Write(byteArray, 0, byteArray.Length); //send the byte array
dataStream.Close(); //close the stream

WebResponse response = request.GetResponse(); //get the response from the web service
string result = new StreamReader(response.GetResponseStream()).ReadToEnd(); //get the Json string from the web service response
CompositeType resultCompositeType = new JavaScriptSerializer().Deserialize<CompositeType>(result); //convert the Json string to a c# object (Car)
Console.WriteLine(resultCompositeType.StringValue); //demonstrate that the object was successfully converted and contains data from the web service

Press Ctrl + F5 to run the program in Visual Studio. If the program is working correctly then a cmd.exe window should open and you should see the text returned from the web service: ‘here is my string value’.

Advertisements