How do you debug ASP.net HTTPHandler

28,691

Solution 1

Open the handler file in Visual Studio and place the breakpoint as you said. Then load the web application in your browser (starting your application in debug-mode of course). If the breakpoint remains gray and doesn't turn filled black, then your handler is probably not registered appropriately in your webapp. That's mostly the issue. If according to you everything is fine, try doing a clean + rebuild of your entire solution. And set your project as startup project(if you're using multiple projects). Often that helps already.

Solution 2

Late reply, but it may serve if you have not found your answer with earlier ones.

If you have created your Handler by copying an existing ashx handler your problem may well be that the markup file of your handler is still pointing to the original handler.

VS by default opens the .ashx.cs file when double-clicking on the .ashx file as the markup file is almost never needed.

By right-clicking on the ashx VS will show option to View Markup, make sure that the Class property is not referring to the old handler.

<%@ WebHandler Language="C#" CodeBehind="MyNewHandler.ashx.cs"
    Class="mynamespace.MyOldHandler" %>

Solution 3

I had this problem and it turns out that the ashx file didn't compile. I guess they get compiled at runtime because I didn't get a compile error until then. Additionally I wasn't seeing the Yellow Screen of Death indicating an HttpCompileException because:

  • I originally intended to fix a bug in the asxh file.
  • I made changes to the file to fix the bug, in which I unknowingly introduced a compile error.
  • I tested the bug fix, got the all purpose friendly exception I had configured in custom errors.
  • I assumed it was the same bug I had gone into fix because I couldn't see the true exception a la Yellow Screen of Death and didn't bother checking ELMAH
  • I set a breakpoint, and couldn't figure out why it wasn't being hit.

Eventually I did see the newly introduced exception, fixed the compile error, and now the breakpoint is being hit like it should, although it doesn't light up when the debugger is initially launched, rather when the handler is invoked (by an AJAX call in my case).

Solution 4

Try do debug using the built in web server instead of the local IIS (or vice-versa if you're using the local IIS). There are minor differences between the two web servers.

Solution 5

I found the method used to debug Windows services also works for the HTTP Handler:

    public void ProcessRequest(System.Web.HttpContext objContext)
    {
// This will automatically launch a dialog that will allow to attach the process to a new or existing instance of Visual Studio
#if DEBUG
     System.Diagnostics.Debugger.Launch();
#endif
[...]

You put the code above in your ProcessRequest method, you build for Debug and deploy on your local machine. Keeping the project open in Visual Studio, when your website invokes the handler, a dialog will pop up allowing to choose that instance. Once you select the instance, switch to VS and you will notice execution is on the System.Diagnostics.Debugger.Launch(); line, waiting for you to continue.

Share:
28,691
MsBao
Author by

MsBao

Updated on July 09, 2022

Comments

  • MsBao
    MsBao almost 2 years

    I have set a breakpoint in an HttpHandler in visual studio. The HttpHandler is an .ashx file I am trying to debug by running my application in the VS development web server. My breakpoint is not getting hit.

    How can I step through it?

    Edit: My breakpoint is not getting hit