Thursday, 14 July 2016

WCF Structure For Consuming Retrofit Network Library in Android

Restful WCF web service structure using Retrofit Network library in Android

Web service design:
For creating web service y WCF you need to have basic knowledge of C#. Create a dynamic project in VS. After that create Interface and implementation class. I have created this for implementing retrofit.
 I have created a WCF web service on visual studio, which has the following structure.
1.      Taking request as JSON and return response as a JSON stream.
Interface Service Class:
 // retrofit test
[OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", UriTemplate = "TestRetroDirectJSON", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    Stream TestRetroDirectJSON(string name, string age, string marks);
Implementation Class:
public Stream TestRetroDirectJSON(string name, string age, string marks)
    {


        M_Test obj = new M_Test();
        obj.name = name;
        obj.age = age;
        obj.marks = marks;

        string str = JsonConvert.SerializeObject(obj);
        byte[] byteArray = Encoding.UTF8.GetBytes(str);
        MemoryStream stream = new MemoryStream(byteArray);
        return stream;
    }

Response return : {"name":"meenakshi","age":"23","marks":"67"}

Poster Test : firstly test it on poster with local host.



2.      Taking request as query parameters and return response as a string

Interface Service Class:
[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "TestRetro_QueryString?name={name}&age={age}", ResponseFormat = WebMessageFormat.Json)]
    string TestRetro_QueryString(string name,string age);

Implementation Class:
public string TestRetro_QueryString(string name,string age)
    {

        byte[] byteArray = Encoding.UTF8.GetBytes("Name="+name+", age="+age);
        MemoryStream stream = new MemoryStream(byteArray);
        return "Name=" + name + ", age=" + age;
    }
Response return: "name=meenakshi,age=23"

 Poster Test: firstly test it on poster with local host.







3.      Taking request as JSON stream and return JSON stream

[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "TestRetroWithStreamBothSide", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    Stream TestRetroWithStreamBothSide(Stream data);

Implementation Class:
public Stream TestRetroWithStreamBothSide(Stream data)
    {

        StreamReader reader = new StreamReader(data);
        string InputJson = reader.ReadToEnd();

        byte[] byteArray = Encoding.UTF8.GetBytes(InputJson);
        MemoryStream stream = new MemoryStream(byteArray);
        return stream;
    }
Response return: {"name":"meenakshi","age":"23","marks":"67"}

Poster Test: firstly test it on poster with local host.



!!!!!!!! Enjoy Happy learning....For any doubt please comment...