Click here to Skip to main content
14,661,493 members
Articles » General Programming » Programming Tips » General
Tip/Trick
Posted 20 Jun 2011

Stats

83.9K views
21 bookmarked

Debug WCF REST Service.

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

Image 1

<%@ 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.

Image 2

This will open Service Instance in web browser like below:

Image 3

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

Image 4

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

Image 5

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

Image 6

Now place debugger in "GetUserDetails" method.

Image 7

And click on "Execute" button in Fiddler.

Image 8

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

Image 9

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

Image 10

You will get result of your Service Request as below:

Image 11

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>


Image 12

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

Image 13

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

Image 14

Now place debugger in "ValidateUser" method.

Image 15

And then click on "Execute" button of Fiddler.

Image 16

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

Image 17

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


Comments and Discussions

 
AnswerSimple and effective solution Pin
Mukesh Salaria6-Jul-14 1:14
MemberMukesh Salaria6-Jul-14 1:14 
GeneralRe: Simple and effective solution Pin
RaisKazi7-Jul-14 11:12
MemberRaisKazi7-Jul-14 11:12 
Question[My vote of 1] Test project fails Pin
msdevtech12-Sep-13 7:10
Membermsdevtech12-Sep-13 7:10 
AnswerRe: [My vote of 1] Test project fails Pin
RaisKazi17-Sep-13 6:54
MemberRaisKazi17-Sep-13 6:54 
GeneralRe: [My vote of 1] Test project fails Pin
msdevtech17-Sep-13 13:10
Membermsdevtech17-Sep-13 13:10 
GeneralMy vote of 3 Pin
Member 977551527-May-13 23:30
MemberMember 977551527-May-13 23:30 
QuestionA small improvement Pin
bytebuster4633-Jun-12 6:00
Memberbytebuster4633-Jun-12 6:00 
AnswerRe: A small improvement Pin
RaisKazi27-Aug-12 5:52
MemberRaisKazi27-Aug-12 5:52 
GeneralReason for my vote of 5 good one Pin
coolsanjay21-Jun-11 4:13
Membercoolsanjay21-Jun-11 4:13 
GeneralRe: Reason for my vote of 5good one Pin
RaisKazi27-Aug-12 5:53
MemberRaisKazi27-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.