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.
[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.
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.
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... ;)