简体   繁体   中英

How to debug startup exceptions with service hosted in IIS?

I have my service running in IIS. I have a non fatal exception occurring and being logged on startup.

How do I attach Visual Studio to IIS so that it also debugs the startup?

I know I can attached visual studio to the w3wp.exe to debug while its running.

Add the following to your application's start up code:

System.Diagnostics.Debugger.Break();

When this line is hit, you will be prompted to attach a debugger to the process. See Debugger.Break for more details.

If you want to live debug you must have the Remote Debugging tools installed on the web server. Make sure to use the correct version for the version of Visual Studio you are running.

Compile your project in debug mode, make sure "Emit debug information" is checked in the Advanced Precompile Settings.

Deploy your project to the server. It is critical that the code in visual studio matches exactly what you deployed. Do not try to debug against even minor differences in code (web.config setting differences are ok).

Run the remote debugging tools on the server with an administrator account. You should see it say "... Waiting for new connections".

In visual studio, use Attach to Process from the Debug menu to attach to the w3wp process on the server. Make sure the Qualifier is the server name : port number that the remote tools is listening on. In some cases, you may need to click the "Select..." button in the Attach to Process window and select only "Managed (v4.5, v4.0)" code types instead of letting it automatically determine code type. If your break points never get hit because no symbols were loaded, this could be why.

Tip: assign a specific app pool to your IIS application so it is easier to identify in the Attach to Process window. You may have a bunch of w3wp.exe processes and you may not know which one you want to attach to.

As an alternative to live debugging, you could run an intellitrace on the server, bring the trace file back into visual studio and step through the code like it is a recorded session. This is a whole other set of instructions involving powershell. I prefer live debugging if it is an option.

UPDATE: To step into Application_Start() , recycle the App Pool and then attach Visual Studio before any requests come in to be sure you are debugging when you (or anyone) makes the first request.

You can also use below code snippet. It is equally handy -

System.Diagnostics.Debugger.Launch();

Wheneever you place it in code, it launches Visual Studio's JIT debugger at that very spot as shown below. As already suggested in the accepted answer you can place this code in application's start-up code at the very first line:

在此输入图像描述

You can then choose and attach to any new or already running instance of Visual Studio across all versions installed on your machine. Hope this helps someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM