Quantcast
Viewing all articles
Browse latest Browse all 14

WCF REST Service with JSON Data

In this post I will show you working with WCF REST Service with JSON type of Data. I will discuss both POST and GET operation on data type JSON .

Creating Service

I have written much post on creating basic WCF REST Service. Please refer them if required.

WebGet Method

Let us create a Get method in WCF REST Service returning JSON type of Data. To return JSON type of data from WCF REST Service, we need to explicitly define message format as JSON in WebGet

Image may be NSFW.
Clik here to view.
clip_image002

In above GetMessageasJSON service

1. RequestFormat is of JSON type

2. ResponseFormat is of JSON type.

3. Above method is returning a string.

WebInvoke Method

Create one more method in WCF REST Service to POST JSON  Data

Image may be NSFW.
Clik here to view.
clip_image004

In above GetMessageasJSON service

1. RequestFormat is of JSON type

2. ResponseFormat is of JSON type.

3. Above method is returning an object of custom class Student.

4. Method is POST

5. Input parameter is an object of custom class Student.

Student is custom class and should be exposed as Data Contract. At client side all the custom classes can be distributed as a separate dll.

Eventually Service and Student class is as below ,

IService1.cs

using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;

namespace JQueryWCFREST
{

[ServiceContract]
public interface IService1
{

[OperationContract]
[WebGet(UriTemplate="/GetJOSNMessage",
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
String GetMessageasJSON();

[OperationContract]
[WebInvoke(UriTemplate = "/UplaodData",
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
Student GetUpdatedStudent(Student s);
}

[DataContract]
public class Student
{
[DataMember]
public string RollNumber { get; set; }
[DataMember]
public string Name { get; set; }
}
}

Service implementation is as below,

Service1.svc.cs

using System;

namespace JQueryWCFREST
{

public class Service1 : IService1
{
public String GetMessageasJSON()
{
return "Hi , I am JOSN DATA ";
}

public Student GetUpdatedStudent(Student s)
{
s.Name = "I have Updated Student Name";
return s;
}
}
}

Hosting Service

Hosting of JSON WCF REST Service can be done in exactly the same way as of default XML WCF REST Service.

Consuming Service

To call above service from ASP.Net or any Managed application, we will have to use below references.

Image may be NSFW.
Clik here to view.
clip_image006

Performing Get Operation

At the client side usual download of data with WebClient will do the work. To De Serialize data, we can use DataContractJSONSerializer

Image may be NSFW.
Clik here to view.
clip_image008

In above method,

1. Data is being downloaded as stream using WebClient

2. De Serialized data using DataContractJSONSerializer

Performing POST Operation

Image may be NSFW.
Clik here to view.
clip_image010

In above method,

1. POST operation is being performed using WebClient

2. Data is being serialized to JSON type using DataContractJsonSeeializer

For reference client side code to perform operation against JSON type WCF REST Service is as below,

Program.cs

using System;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using JQueryWCFREST;

namespace JSONClient
{
class Program
{
static void Main(string[] args)
{
#region Getting JOSN Data
WebClient proxy = new WebClient();
byte[] data = proxy.DownloadData( "http ://localhost:38395/Service1.svc/GetJosnmessage)
Stream stream = new MemoryStream(data);
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
string result = obj.ReadObject(stream).ToString();
Console.WriteLine(result.ToString());
Console.ReadKey(true);

#endregion

#region Uploading JOSN Data
Student student = new Student{Name ="Dhananjay",RollNumber ="9"};
WebClient Proxy1 = new WebClient();
Proxy1.Headers["Content-type"] = "application/json";
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer serializerToUplaod = new DataContractJsonSerializer(typeof(Student));
serializerToUplaod.WriteObject(ms, student);
data = Proxy1.UploadData(http://localhost:38395/Service1.svc/UplaodData", "POST", ms.ToArray());
stream = new MemoryStream(data);
obj = new DataContractJsonSerializer(typeof(Student));
var resultStudent = obj.ReadObject(stream) as Student;
Console.WriteLine(resultStudent.RollNumber+" " + resultStudent.Name);
Console.ReadKey(true);

#endregion

}
}
}

On running output expected is,

Image may be NSFW.
Clik here to view.
clip_image012


Filed under: REST Services Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 14

Trending Articles