转摘:http://www.codeproject.com/Articles/37182/Debug-your-ASP-NET-Application-while-Hosted-on-IIS

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 ProcessApplication 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.

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.

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.

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.

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.

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.

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.

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

    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.

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.

Fig. 10: Task Manager showing the running IIS process

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

Fig. 11: Opening the Attach to Process window

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

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:

Fig. 13-1: Process attached successfully

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 mySampleWebSite. Follow the same steps as before. Open the Process Attach window:

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 = 4308and 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:

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 ProcessProcess ID and Application Pool Name!

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.

Fig. 17: Attach the correct process for debugging

Now, enjoy debugging!

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)

Debug your ASP.NET Application while Hosted on IIS的更多相关文章

  1. 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 ...

  2. [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 ...

  3. ASP.NET Application Life Cycle

    The table in this topic details the steps performed while an XAF ASP.NET application is running. Not ...

  4. 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 ...

  5. How to increase timeout for your ASP.NET Application ?

    How to increase timeout for your ASP.NET Application ? 原文链接:https://www.techcartnow.com/increase-tim ...

  6. [转]ASP.NET Core 1 Deploy to IIS

    本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...

  7. 将asp.net core站点发布到IIS上遇到的问题

    今天第一次将整个 asp.net core 站点发布到 IIS 上,以前都是发布到 Linux 服务器上. 开始使用 dotnet publish -c release 命令发布,用浏览器访问站点时出 ...

  8. Application Initialization Module for IIS 7.5

    http://www.iis.net/downloads/microsoft/application-initialization IIS7.5也有Warm Up功能 让ASP.NET第一次Reque ...

  9. 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服务器 ...

随机推荐

  1. eclipse安装weblogic Server服务器

    1.首先打开eclipse,第一次进入欢迎画面点击上方标签X,关闭欢迎标签 2.关闭欢迎标签后,进入eclipse操作界面,在上方的菜单栏,选择windows下拉菜单,选择子菜单Preference ...

  2. Mac版Navicat Premium激活教程

    工具: Navicat Premium12.0.20 安装包 下载注册机工具包 链接:https://pan.baidu.com/s/1NS8gk780ds1Xn-zHrSIzIw  密码:dvke ...

  3. 屏幕坐标点转UGUI坐标【包含屏幕适配】

    using UnityEngine; public class ScreenToUI : MonoBehaviour { public const float UI_Width = 1366f; pu ...

  4. maven system path,加载本地jar

    当引用第三方包,且没有源代码时候,可以使用system path <dependency> <groupId>ctec</groupId> <artifact ...

  5. python正则表达式 re (二)sub

    背景: re.sub是re模块重要的组成部分,并且功能也非常强大,主要功能实现正则的替换. re.sub定义: sub(pattern, repl, string, count=0, flags=0) ...

  6. 一、Angular环境的搭建

    1.安装nodejs (1) 下载网址https://nodejs.org/en/download/ (2) 双击进行安装 (3) 打开命令行,输入node -v 和 npm -v 查看是否安装成功 ...

  7. mac终端方式修改host

    打开终端 cd /      #进入根目录 ls      #查看根目录下列表,确定有需要打开的目录 cd etc   #进入配置文件目录 ls sudo vim hosts      #用vim打开 ...

  8. c++ ofstream使用方法

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,流缓冲即是内存空间. 插入器<<  : 向流输出数据. cout << "test!" &l ...

  9. shell脚本特殊变量与变量子串相关知识

    一.shell脚本特殊变量 1.shell中常用特殊位置变量说明: $0 获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,那么就包含了脚本路径 $n 获取当前执行的shell脚本的第n个 ...

  10. modelviewset 中的序列化方法怎么用