Call in WCF Service

Recently I was working on a WCF service in which the requirements included creation of a restful WCF service that an Android application will consume as well as invocation of the same service in a .NET web application. I am assuming here that readers have a basic idea of what a WCF REST service is.

My service returned data in JSON format when I was invoking it from mobile applications. It was working fine but in .NET applications I was using an ajax call via jQuery; it was giving the error "Cross Platform Invocation". I tried with JSONP too but as expected that did not work either so I decided to call it from the C# code I am providing and explaining here.

My service contract  has one GET Request function returning a class in JSON format that is as follows:

[OperationContract]

[WebGetAttribute(UriTemplate = "/Track/{Sno}",

BodyStyle = WebMessageBodyStyle.WrappedRequest,

RequestFormat = WebMessageFormat.Json,

ResponseFormat = WebMessageFormat.Json)]

Tracker Track(string Sno)         

And my data contract Tracker class is as follows:
 

[DataContract]

public class Tracker

{

    [DataMember]

    public string Origin { get; set; }

 

    [DataMember]

    public string Destination { get; set; }

 

    [DataMember]

    public string NumberPiece { get; set; }

 

    [DataMember]

    public string GrossWeight { get; set; }

 

    [DataMember]

    public string VolumeWight { get; set; }

 

    [DataMember]

    public string ChargableWeight { get; set; }

 

    [DataMember]

    public string ConsigneeName { get; set; }

 

    [DataMember]

    public string ShipperName { get; set; }

}
 
The logic is simple in the service class function based on the sno I was executing a query in the database binding it to the class and returning it.
 
Until now we have talked about the service. From here the actual game starts. In the calling application I made a class and declared a field, Sno, that will be sent to the service.
 

public class WebTracking

{

    public string Sno { get; set; }

}

Now, since the service is returning a class with string values, to receive it I made one more class and declared all the fields that I declared in the Tracker class of the service as in the following:

public class ResponseObj

{

    public string Origin { get; set; }

    public string Destination { get; set; }

    public string NumberPiece { get; set; }

    public string GrossWeight { get; set; }

    public string VolumeWight { get; set; }

    public string ChargableWeight { get; set; }

    public string ConsigneeName { get; set; }

    public string ShipperName { get; set; }

}
 
Now to call the service and receive the data. Let's make one more class here with one function as in the following:
 

public class TrackingRequest

{

    public ResponseObj GetTracking(WebTracking info)

    {

        ResponseObj recoveredList = null;

        string uri = "http://localhost/Tracking/Track.svc/TrackShipment/" + info.Sno;

        HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;

        req.KeepAlive = false;

        req.Method = "GET";

        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

        Encoding enc = System.Text.Encoding.GetEncoding(1252);

        StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), enc);

        string Response = loResponseStream.ReadToEnd();

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        recoveredList = serializer.Deserialize<ResponseObj>(Response);

        loResponseStream.Close();

        resp.Close();

        return recoveredList;

    }

}


Since the service is on a local IIS and the method is of a GET REQUEST type I am sending the parameter in the URL,calling the service with HTTP WEBREQUEST and receiving the response in a HTTP WEB RESPONSE object. In the next step I am reading the response stream into a stream reader object and converting it into the string. Until now the exact JSON string is retrieved. Now to convert this JSON string into the Class response object  I deserialize it and assigned it to the ResponseObject's object and returned it.

Call it from the page as in the following:
 

WebTracking wt=new WebTracking();

Wt.Sno="30";

ResponseObj Obj=new ResponseObj();

TrackingRequest Tr=new TrackingRequest();

Obj=Tr. GetTracking(wt);

 

In the next article I will explain how to POST data to the service and receive the response .
 

Up Next
    Ebook Download
    View all
    Learn
    View all