Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new to WCF REST Web Services
I have write a rest based web service for complaining. Some attributes are ComplainType, ComplainBody, Image etc.
So what i want is to call this service via a android and from website and parameter should be Complain Object type.

So what is the way to do this. ?
I'm Fed up of searching this :(
thank you
Posted

1 solution

Ok. Finally I found a solution by myself after talking with my friend. :D. So let's look at how to send a Complex type over WCF REST Web Service.

So let's think that there is a Interface class which contain MakeComplain and this method take a complain object as a parameter. and it's return a boolean value to ensure that the object received.
C#
[ServiceContract]
    public interface IComplainService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat=      WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
        bool MakeCompalin(Complain comp);
    } 


So, Let's look at the Implementation of the Interface class. In hear we check whether the object is received.
C#
public class ComplainService : IComplainService
    {
        public bool MakeCompalin(Complain comp)
        {
            if (comp != null)
            {                               
                return true;
            }
            else
            {
                return false;
            } 
        }
    }


I hope everyone knows how to do the configuration part in web.config file. I think i don't want to mention it hear. ;)

Now Let's create a Client Project to test the Service. I have created a console application to test the service.
C#
class Program
    {
        static void Main(string[] args)
        {
            Complain complain = new Complain()
            {
                CompainType = "type1",
                CompainBody = "Body1"
            };
            
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string output = serializer.Serialize(complain);

            string strUri = "http://localhost:6595/ComplainService.svc/MakeCompalin";
            Uri uri = new Uri(strUri);
            WebRequest request = WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";

            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            string serOut = jsonSerializer.Serialize(complain);

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(serOut);
            }

            WebResponse responce = request.GetResponse();
            Stream reader = responce.GetResponseStream();

            StreamReader sReader = new StreamReader(reader);
            string outResult = sReader.ReadToEnd();
            sReader.Close();
        }


In hear what i'm doing is communicate to service via POST method. First i have to serialize in to jason message using JavaScriptSerializer and write that into writer which is created using StreamReader. I hope rest of the things u guys know.. ;)
So that's all :D

this may not be the ideal method to do this. If there are any other methods. Please do share with us... ;)
 
Share this answer
 
Comments
[no name] 20-Mar-15 1:40am    
Hi,

i tried you approach to a pass complain object.but object value going as null in service.

Can you please let me know the issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900