Please refer to the WCF Rest Service we created in the earlier post

<<earlier post link>>

Here we'd call the GetDatUsingDataContract operation which accepts a CompositeType

 

In the previous example we should set the the NameSpace for the DataContract. Here we are setting it to be blank.

 

Using Web Request

 

WebRequest req = WebRequest.Create(@"http://localhost:64538/Service1.svc/getdatacontract");

req.Method = "POST";

req.ContentType = @"application/xml; charset=utf-8";

 

StringBuilder builder = new StringBuilder();

builder.AppendLine("<CompositeType>");

builder.AppendLine("<BoolValue>true</BoolValue>");

builder.AppendLine("<StringValue>test string</StringValue>");

builder.AppendLine("</CompositeType>");

 

req.ContentLength = Encoding.UTF8.GetByteCount(builder.ToString());

 

using (Stream stream = req.GetRequestStream())

{

stream.Write(Encoding.UTF8.GetBytes(builder.ToString()), 0, Encoding.UTF8.GetByteCount(builder.ToString()));

}

 

HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

MessageBox.Show(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode,

resp.StatusDescription));

 

Using WebChannelFactory

 

Add reference to the following assemblies in the Client Application

TestWCFServiceApp è WCF Project Assembly. (Here basically all we need is the contract – ServiceContract and DataContract class used by the WCF Service i.e. IService1 class)

 

 

Source Code:

WebChannelFactory<IService1> webChannelFactory = new WebChannelFactory<IService1>(

new Uri("http://localhost:64538/Service1.svc"));

IService1 client = webChannelFactory.CreateChannel();

 

CompositeType compositeType = new CompositeType();

compositeType.BoolValue = true;

compositeType.StringValue = "Test String";

 

CompositeType resCompositeType = client.GetDataUsingDataContract(compositeType);

 

Hope it helps.