Create client to test WCF Rest Service

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&#8221;);

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&#8221;));


IService1 client = webChannelFactory.CreateChannel();


CompositeType compositeType = new
CompositeType();

compositeType.BoolValue = true;

compositeType.StringValue = “Test String”;


CompositeType resCompositeType = client.GetDataUsingDataContract(compositeType);

Hope it helps.

Debugging WCF Rest Service

 

Here we will see how we can debug a WCF Rest Service using Advanced Rest Client (Chrome Extensions)

https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

Suppose this is our sample WCF Rest Service we want to debug

(Please check the following post on details of creating a WCF rest service <<earlier posts link>>)

The Advanced Rest Client interface

  • Here we have specified Url of the Rest Service
  • The Post Data to be passed
  • The response that we got back

Now to debug our WCF Rest Service set the debugging point and attach the debugger to the Advanced Rest Client process.

 

Run the WCF Service in the Visual Studio.

Specify the WCF Rest Service URL and Post Data expected and then click on Send on the Advanced Rest Client.

The debugging will start

Hope it helps.