Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Debug WCF REST Service.

4.91/5 (13 votes)
21 Jun 2011CPOL2 min read 96.5K  
Download WCFRESTCODE.zip

In this article, we will see How to Debug WCF REST Service.

Prerequisite: Fiddler should be installed.
http://www.fiddler2.com/Fiddler2/version.asp


Refer to the attached sample WCF REST Service Project.

  • Add reference of "Microsoft.ServiceModel.Web.dll" from binary folder of Project and build the Project.
  • Host service in your IIS.
  • See how Service.svc is modified by viewing its Markup



<%@ ServiceHost Language="C#" Debug="true" Service="WCFRESTCODE.Service"  Factory="WCFRESTCODE.AppServiceHostFactory" %>

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Microsoft.ServiceModel.Web;
 
namespace WCFRESTCODE
{
    class AppServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            WebServiceHost2 webServiceHost2 = new WebServiceHost2(serviceType, true, baseAddresses);
 
            return webServiceHost2; 
        }
    }
}


Now we start with the actual debugging steps.

GET Method – Let’s see how to debug GET method.

In solution explorer, right click on your WCF REST Service Project. Then select "Start new instance" option under Debug menu.



This will open Service Instance in web browser like below:



You may get the below message on web page. Don’t bother and leave this page as it is open.

"Error Description: 'Resource does not exist'
This may be because an invalid URI or HTTP method was specified. Please see the service help page for constructing valid requests to the service."


Now open Fiddler. Select "GET" option from dropdown.

To debug method "GetUserDetails"[GET], copy below URL in Fiddler.

"http://localhost/WCFRESTCODE/Service.svc/GetUserDetails/123"

Why this format of URL?

* Because we have specified below format for "UriTemplate" Property of "WebInvoke" Attribute of "GetUserDetails" method.

/GetUserDetails/{userId}

Add below entry in "Request Headers" section.
Content-Type: text/xml



Now in Visual Studio, click on "Attach to Process…" option under "Debug" Menu.



Then select "Fiddler.exe" in "Available Processes" section and click on "Attach" button.



Now place debugger in "GetUserDetails" method.



And click on "Execute" button in Fiddler.



This will bring debugger pointer in your "GetUserDetails" method.



Now go to Fiddler and double click on your Service Request in Left Hand Panel.



You will get result of your Service Request as below:



POST Method

In solution Explorer, Right click on your WCF REST Service Project. Then select "Start new instance" option under Debug menu. As we did for GET method debugging steps.
Open Fiddler. We have to specify REST URL and its POST Web method which we want to debug.

"http://localhost/WCFRESTCODE/Service.svc/ValidateUser"

Paste this URL in fiddler and select "POST" option from dropdown.

Add below text in "Request Headers" Tab.

Content-Type: text/xml


Now let’s specify input.

We are passing object of a class "UserInput" and we have specified "RequestFormat=WebMessageFormat.Xml" so that input will be like below:

XML
<UserInput>
<Password>samplepassword</Password>
<UserId>sampleuser</UserId>
</UserInput>




In Visual Studio, click on "Attach to Process…" option under "Debug" Menu.



Then select "Fiddler.exe" and click on "Attach" button.



Now place debugger in "ValidateUser" method.



And then click on "Execute" button of Fiddler.



This will bring you in debugging mode of "ValidateUser" method.



Have a Happy WCF REST Service Programming and Debugging.

License

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