Click here to Skip to main content
15,878,945 members
Articles / Web Development / ASP.NET

Debug your ASP.NET Application while Hosted on IIS

Rate me:
Please Sign up or sign in to vote.
4.87/5 (119 votes)
15 Jul 2009CPOL9 min read 669.7K   229   94
This article describes how to debug a web application which is hosted on IIS. It also describes how to select the correct process to attach to when multiple worker processes are running

Contents

Overview

Generally we debug our ASP.NET web application from Visual Studio. Visual Studio has its own ASP.NET engine, which is capable enough to run and debug your web sites inside Visual Studio. However, if your site is hosted on IIS and you want to debug that site directly, how would you debug it? When we host sites on IIS, the Worker Process (w3wp.exe) is used to run the web application. We need to attach to this particular process from Visual Studio to debug the web application. This article describes the overall idea of debugging an application using this method. It also describes the Worker Process, Application Pool and selecting a particular process if there are multiple Worker Processes running on IIS, using iisapp.vbs. I hope you will enjoy this article and provide your valuable suggestions and feedback.

ASP.NET Debugging vs. IIS Debugging

Visual Studio has its own integrated debugging engine, which debugs our code when we run the application in Visual Studio. If we are developing a site and need to debug the code, we just set breakpoints and do the debugging (Note: In this article I do not describe how to set the debug mode).

When we run the application, execution breaks when certain a breakpoint is reached. It is very simple, because when an ASP.NET application is running in Visual Studio, it is under the control of the ASP.NET Engine which is integrated with Visual Studio. If you want to check which process is running for debugging, run the web application from Visual Studio: you will get a popup notification as shown below.

Image 1

Fig. 1: Taskbar popup when debugging is started from Visual Studio

This indicates a process is starting to run the ASP.NET application. Double-click on the icon and a popup window will appear to show the details.

Image 2

Fig. 2: Development Server process details

Behind the running process is WebDev.WebServer.exe. When We press F5 to run the application, this process starts to execute the it. If you want run the application from command prompt, you have to perform the following steps.

Steps to run a web application from the command prompt:

  1. Open The Visual Studio command prompt
  2. Run WebDev.WebServer

The following screen will come up. Check the Example section there.

Image 3

Fig. 3: WebDev.WebServer usage notification

Now back to IIS debugging. IIS comes into the picture when we deploy or host the site. After deploying the site on IIS, if we want to debug the site there, we can't do it directly as in Visual Studio. IIS has its own Worker Process which takes care of all execution and maintenance of deployed web applications. I will describe the details of the Worker Process in a later section. So, if we have running process in IIS and we need to debug the application, first of all we have to attach to the correct process from Visual Studio. Before describing that, let's just have a look at the Worker Process and Application Pool.

What is the Worker Process?

The Worker Process (w3wp.exe) runs ASP.NET applications within IIS. All ASP.NET functionality runs under the scope of the Worker Process. When a request comes to the server from a client, the Worker Process is responsible for generating the request and response. Its also maintains the InProc session data. If we recycle the Worker Process, we will lose its state. For more information, read this article: A Low-Level Look at the ASP.NET Architecture

Application Pool

This is one of the most important things that you should create for your own application in a production environment. Application Pools are used to separate sets of IIS Worker Processes that share the same configuration. Application Pools enable us to isolate our web application for better security, reliability, and availability. The Worker Process serves as the process boundary that separates each Application Pool, so that when one Worker Process or application has an issue or recycles, other applications or Worker Processes are not affected.

Image 4

Fig. 4: Relationship between Application Pool and worker process in IIS

Default Application Pool

The name of the default application of IIS 6.0 is DefaultAppPool. After hosting the site on IIS, if we check the properties of the virtual directory, we can to view that information as follows.

  1. Start Menu → Run command → inetmgr
  2. Expand DefaultWebSites or Other Web Sites, where you have created the virtual directory
  3. Right Click on the virtual directory
  4. Click on Properties

The following virtual directory properties screen will come up, showing the Application Pool name which is assigned to the selected site.

Image 5

Fig. 5: Virtual directory properties showing Application Pool name

If you want to check the list of all Application Pools in IIS, you have to expand the Application Pool node on IIS Server.

Image 6

Fig. 6: List of Application Pools

Now, each and every Application Pool should have the minimum of one Worker Process which takes care of the operation of the site which is associated with the Application Pool. Right-click on the Application Pool → go to the Performance tab, check near the bottom of the tab, there is a web garden section, and by default, the number of Worker Processes is 1. An Application Pool containing more than one Worker Process called a Web Garden.

Image 7

Fig. 7: Application Pool properties showing Web garden

Creating and Assigning an Application Pool

  • Open the IIS Console, right-click on the Application Pools folder, select New

    Image 8

    Fig. 8-1
  • Give the Application Pool ID and click OK.

    Image 9

    Fig. 8-2
  • Now, right-click on the virtual directory and assign the newly created Application Pool to that virtual directory.

    Image 10

    Fig. 8-3

Now, this web site will run independently, within StateServerAppPool, so any problem related to other applications will not affect this application. This is the main advantage of creating a separate Application Pool.

How to start?

What I have said up to now give you a good idea of Worker Processes and Application Pools. You should have a clear understanding on these before going on to the next part. Now I will show you how to debug a site which is hosted on an IIS Server.

For the demonstration, I have created a web site called SampleWebSite and hosted it on to my local IIS. Below is default page output.

Image 11

Fig. 9: Sample web site

Which process to attach to?

Now, as I have already explained, the process name is w3wp.exe, so we can check it from our Task Manager whether or not the Worker Process is running.

Image 12

Fig. 10: Task Manager showing the running IIS process

Now we are going to attach to the process. In Visual Studio, go to DebugAttach to Process

Image 13

Fig. 11: Opening the Attach to Process window

After clicking Attach to Process, the following screen will come up

Image 14

Fig. 12: Attach to Process window, showing a single Worker Process running

Now we are able to see that the Worker Process is running, and we need to attach that process. Select the process and click on the Attach button. After that, look at the two images below:

Image 15

Fig. 13-1: Process attached successfully

Image 16

Fig. 13-2: Process not attached

Did you notice the breakpoint symbol? If the Worker Process attached successfully, within the code, the breakpoint symbol should be a solid circle. Otherwise it will have a warning icon as shown. For a single Worker Process, this scenario is not common. However, when we have multiple Worker Processes running on IIS, then we can have some confusion. I will discuss the same in a later section.

Now if we click the Debug button after successfully attaching to the process, execution will stop at the breakpoint.

Next, let's have a look at what to do if we have multiple Worker Processes running.

How to attach to one of many running Worker Processes

Now, when this scenario will come up? When we have multiple sites hosted on IIS, and those sites have their own Application Pool. Now, multiple Application Pools means multiple Worker Processes are running.

Here I have three Application Pools in my IIS. They are:

  1. Default Application Pool
  2. Generic Application Pool
  3. State Server Application Pool

Now, my SampleWebSite is associated with the DefaultAppPool. Now, I want to attach the process to debug my SampleWebSite. Follow the same steps as before. Open the Process Attach window:

Image 17

Fig. 14: List of multiple Worker Process

Just have a look, there are three Worker Processes currently running, and you have to attach one of them. But, you do not know which Worker Process is the default Application Pool's. What you do is, you select any one of them at random, let's say the one with process ID = 4308, and suppose it is not the Worker Process for the default Application Pool. So what will happen if you attach to a wrong process? Check the image below:

Image 18

Fig. 15: Breakpoint when the process is not attached correctly

Getting a list of running Worker Processes

Now what is the solution for the previous case? Here is a quick tip:

  • Start → Run command → cmd
  • Change directory to \Windows\System32
  • Run the command: cscript iisapp.vbs

and wait for the output. Wow! You get a list of running Worker Process, Process ID and Application Pool Name!

Image 19

Fig. 16: List of running Worker Processes with PID and Application Pool name

Attaching to the correct process

From here you can easily identify the correct Application Pool name and its process ID. Now, return to Visual Studio → Attach Process. Now you know that the process ID for Default Application Pool is 1772, so Attach to that process.

Image 20

Fig. 17: Attach the correct process for debugging

Now, enjoy debugging!

Image 21

Fig. 18: Breakpoint is ready

Summary

Sometimes we need to debug our application which is hosted on IIS. For that, we need to attach the running Worker Process to the Visual Studio. If we have multiple Worker Processes running on the IIS server, we can identify the appropriate Worker Process by using the command cscript iisapp.vbs.

I hope this article will help beginners who are still struggling with debugging applications that are hosted on IIS. Please give your feedback and suggestions to improve the article.

Thank you.

License

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


Written By
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

Comments and Discussions

 
QuestionUnable to debug Pin
ashishtanwar8816-Jun-17 1:25
ashishtanwar8816-Jun-17 1:25 
GeneralMy vote of 4 Pin
Manas_Kumar21-Jan-16 19:07
professionalManas_Kumar21-Jan-16 19:07 
QuestionDebug your ASP.NET Application while Hosted on IIS Pin
JamesHayden6-Dec-15 16:18
JamesHayden6-Dec-15 16:18 
GeneralMy vote of 1 Pin
serebrem26-Jul-15 17:23
serebrem26-Jul-15 17:23 
GeneralGreat Article Pin
Dhananjay Singh Parihar19-May-15 5:54
Dhananjay Singh Parihar19-May-15 5:54 
Questionattach your own windows service to IIS Pin
shubhangipatil26-Aug-14 3:19
shubhangipatil26-Aug-14 3:19 
QuestionUnable to Debug ASP.NET Application while Hosted on IIS Pin
madhusudhan.k21-Jul-14 4:25
madhusudhan.k21-Jul-14 4:25 
QuestionNice article. Pin
Dixit Mirtkar28-Jan-14 0:02
Dixit Mirtkar28-Jan-14 0:02 
QuestionClear article but please update it to the latest version Pin
sharp_k15-Jan-14 10:17
sharp_k15-Jan-14 10:17 
QuestionUnable to find the process in Visual Studio Attach Process Pin
chandu_vrs6-Nov-13 23:29
chandu_vrs6-Nov-13 23:29 
QuestionThanks Pin
mehr4n mousavi1-Aug-13 14:07
professionalmehr4n mousavi1-Aug-13 14:07 
Questionfeedbak for the article Pin
itzzmearun29-Jan-13 19:48
itzzmearun29-Jan-13 19:48 
QuestionUse File- > "open website" ->choose site from IIS directly Pin
rama charan20-Jan-13 15:35
rama charan20-Jan-13 15:35 
QuestionCan debug asp.net web application like this but asp.net website still show the message on breakpoint "Breakpoint will not be hit.No symbols loaded for this document." Pin
prachich17-Jan-13 21:41
prachich17-Jan-13 21:41 
Questionable to debug asp.net web application like this but asp.net website still show the message on breakpoint "Breakpoint will not be hit.No symbols loaded for this document." Pin
prachich17-Jan-13 21:39
prachich17-Jan-13 21:39 
GeneralMy vote of 5 Pin
Prasyee25-Sep-12 3:48
Prasyee25-Sep-12 3:48 
QuestionThank you! Pin
Rodrigo24520-Sep-12 1:23
Rodrigo24520-Sep-12 1:23 
GeneralMy vote of 5 Pin
Maharajan.N28-Aug-12 0:13
Maharajan.N28-Aug-12 0:13 
Graet
GeneralMy vote of 4 Pin
JustinoScalabitano21-Jun-12 0:32
professionalJustinoScalabitano21-Jun-12 0:32 
GeneralRe: My vote of 4 Pin
Vitaly Tomilov28-Jun-12 3:48
Vitaly Tomilov28-Jun-12 3:48 
GeneralMy vote of 5 Pin
Sandesh M Patil8-Jun-12 2:46
Sandesh M Patil8-Jun-12 2:46 
QuestionDebugging WCF Service? Pin
foluis4-Jun-12 6:38
foluis4-Jun-12 6:38 
QuestionGreat work Pin
parveen_gkv22-Mar-12 21:07
parveen_gkv22-Mar-12 21:07 
QuestionGreat article, but I can't find "properties" anywhere in IIS. Pin
Graf Donnerblitzen19-Jan-12 11:02
Graf Donnerblitzen19-Jan-12 11:02 
GeneralGood job Pin
Mastersev12-Oct-11 8:20
Mastersev12-Oct-11 8:20 

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.