Debug your ASP.NET Application while Hosted on IIS
转摘:http://www.codeproject.com/Articles/37182/Debug-your-ASP-NET-Application-while-Hosted-on-IIS
Contents
- Overview
- ASP.NET Debugging vs. IIS Debugging
- What is the Worker Process?
- Application Pool
- How to start?
- Which process to attach to?
- How to attach to one of many running Worker Processes
- Summary
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.

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.

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:
- Open The Visual Studio command prompt
- Run WebDev.WebServer
The following screen will come up. Check the Example section there.

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.

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.
- Start Menu → Run command →
inetmgr - Expand DefaultWebSites or Other Web Sites, where you have created the virtual directory
- Right Click on the virtual directory
- Click on Properties
The following virtual directory properties screen will come up, showing the Application Pool name which is assigned to the selected site.

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

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.

Creating and Assigning an Application Pool
- Open the IIS Console, right-click on the Application Pools folder, select New
Fig. 8-1 - Give the Application Pool ID and click OK.
Fig. 8-2 - Now, right-click on the virtual directory and assign the newly created Application Pool to that virtual directory.
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.

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.

Now we are going to attach to the process. In Visual Studio, go to Debug → Attach to Process

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

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:


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:
- Default Application Pool
- Generic Application Pool
- State Server Application Pool
Now, my SampleWebSite is associated with the DefaultAppPool. Now, I want to attach the process to debug mySampleWebSite. Follow the same steps as before. Open the Process Attach window:

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:

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!

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.

Now, enjoy debugging!

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)
Debug your ASP.NET Application while Hosted on IIS的更多相关文章
- VS2015/2013/2012 IIS Express Debug Classic ASP
参考资料: https://msdn.microsoft.com/en-us/library/ms241740(v=vs.100).aspx When you attach to an ASP Web ...
- [Angular 2] How To Debug An Angular 2 Application - Debugging via Augury or the Console
In this lesson we will learn several ways to debug an Angular 2 application, including by using Augu ...
- ASP.NET Application Life Cycle
The table in this topic details the steps performed while an XAF ASP.NET application is running. Not ...
- ASP.NET Application and Page Life Cycle
ASP.NET Application Life Cycle Overview for IIS 7.0 https://msdn.microsoft.com/en-us/library/bb47025 ...
- How to increase timeout for your ASP.NET Application ?
How to increase timeout for your ASP.NET Application ? 原文链接:https://www.techcartnow.com/increase-tim ...
- [转]ASP.NET Core 1 Deploy to IIS
本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...
- 将asp.net core站点发布到IIS上遇到的问题
今天第一次将整个 asp.net core 站点发布到 IIS 上,以前都是发布到 Linux 服务器上. 开始使用 dotnet publish -c release 命令发布,用浏览器访问站点时出 ...
- Application Initialization Module for IIS 7.5
http://www.iis.net/downloads/microsoft/application-initialization IIS7.5也有Warm Up功能 让ASP.NET第一次Reque ...
- asp.net core 2.1 部署IIS(win10/win7)
asp.net core 2.1 部署IIS(win10/win7) 概述 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器 ...
随机推荐
- Java 时间类 Date 和 Calendar
在项目中获取一个yyyy-MM-dd HH:mm:ss格式的时间字符串 package org.htsg.kits; import java.text.SimpleDateFormat; import ...
- Mongodb使用shell实现与javascript的动态交互
关于利用mongodb的shell执行脚本,这点在以前的文章中有点遗漏:现在在此篇博客中做个补充: 一.在命令行中传入脚本文件 定义一个javasciprt文件,名称为:script1.js,内容如下 ...
- HashMap、HashTable、ConcurrentHashMap区别
HashMap与HashTable区别 HashMap与ConcurrentHashMap区别 1.HashMap与HashTable的区别 HashMap线程不安全,HashTable线程安全 Ha ...
- 单例设计模式(Singleton)的优化
单例模式的优化 单例模式懒汉式写法,单例模式的优化有以下四个方面: 使用同步保证线程安全synchronized 使用volatile关键字:volatile关键字提醒编译器后面所定义的变量随时都有可 ...
- 4412 gpio读取pwm
一.可以使用的GPIO管脚 去掉占用调用的GPIO驱动,包括leds,buzzer,camera ov5640,WIFI mt6620 ,Keyboards VIDEO_OV5640– Device ...
- 【Linux】【Kibana】解决Kibana启动失败:Data too large问题
今天重启Kibana容器,结果启动不了,一看日志发现是Data数据量太大报错. FATAL [circuit_breaking_exception] [parent] Data too large, ...
- 【HDOJ6604】Blow up the city(支配树)
题意:给定一个n点m边的DAG,将只有入边的点称为周驿东点 q次询问,每次给定a,b两点,询问删去某个点x和其相连的所有边,能使a,b至少其中之一不能到达任何周驿东点的x的个数 n,q<=1e5 ...
- 微信小程序相关操作
显示用户基本信息 在微信小程序中,经常会碰到需要展示微信用户的基本信息,如果只是为了显示用户信息,最简单有效的办法是使用open-data,这是微信小程序内置的用于展示微信开放数据的组件,通过改变ty ...
- 在Windows系统使用Gpg4win进行加密解密
GPG,又称为GnuPG,全称是Gnu Private Guard,即GNU隐私卫士.GPG是以PGP算法为核心的强大的加密软件.但GPG项目是一套命令行程序,而且是为 Linux 等开源操作系统设计 ...
- Adobe Premiere
工具 移动工具(Selection Tool)快捷键(V) 最最常用的工具,常规功能是移动素材以及控制素材的长度 配合 ctrl:可以拖拽素材,移动到切入点进行插入 配合 shift:选择多目标(可以 ...