Click here to Skip to main content
16,018,394 members
Articles / Web Development / ASP.NET
Tip/Trick

Debug WCF REST Service.

Rate me:
Please Sign up or sign in to vote.
4.91/5 (13 votes)
21 Jun 2011CPOL2 min read 96.4K   21   10
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)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerSimple and effective solution Pin
Mukesh Salaria6-Jul-14 1:14
Mukesh Salaria6-Jul-14 1:14 
GeneralRe: Simple and effective solution Pin
RaisKazi7-Jul-14 11:12
RaisKazi7-Jul-14 11:12 
Your article is incomplete. Where are the examples of debugging of Get and POST methods?
Change is a pattern of life.

Question[My vote of 1] Test project fails Pin
msdevtech12-Sep-13 7:10
msdevtech12-Sep-13 7:10 
AnswerRe: [My vote of 1] Test project fails Pin
RaisKazi17-Sep-13 6:54
RaisKazi17-Sep-13 6:54 
GeneralRe: [My vote of 1] Test project fails Pin
msdevtech17-Sep-13 13:10
msdevtech17-Sep-13 13:10 
GeneralMy vote of 3 Pin
Member 977551527-May-13 23:30
Member 977551527-May-13 23:30 
QuestionA small improvement Pin
bytebuster4633-Jun-12 6:00
bytebuster4633-Jun-12 6:00 
AnswerRe: A small improvement Pin
RaisKazi27-Aug-12 5:52
RaisKazi27-Aug-12 5:52 
GeneralReason for my vote of 5 good one Pin
coolsanjay21-Jun-11 4:13
coolsanjay21-Jun-11 4:13 
GeneralRe: Reason for my vote of 5good one Pin
RaisKazi27-Aug-12 5:53
RaisKazi27-Aug-12 5:53 

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

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