Click here to Skip to main content
Click here to Skip to main content

Debug WCF REST Service.

, 21 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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:
 
<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)

Share

About the Author

RaisKazi
Technical Lead
United States United States
No Biography provided

Comments and Discussions

 
AnswerSimple and effective solution PinmemberMukesh Salaria6-Jul-14 1:14 
GeneralRe: Simple and effective solution PinmemberRaisKazi7-Jul-14 11:12 
Question[My vote of 1] Test project fails Pinmembermsdevtech12-Sep-13 7:10 
AnswerRe: [My vote of 1] Test project fails PinmemberRaisKazi17-Sep-13 6:54 
GeneralRe: [My vote of 1] Test project fails Pinmembermsdevtech17-Sep-13 13:10 
GeneralMy vote of 3 PinmemberMember 977551527-May-13 23:30 
QuestionA small improvement Pinmemberbytebuster4633-Jun-12 6:00 
AnswerRe: A small improvement PinmemberRaisKazi27-Aug-12 5:52 
GeneralReason for my vote of 5 good one Pinmembercoolsanjay21-Jun-11 4:13 
GeneralRe: Reason for my vote of 5good one PinmemberRaisKazi27-Aug-12 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 21 Jun 2011
Article Copyright 2011 by RaisKazi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid