Debugging Windows Service Tip

This is a quick tip on debugging Windows services or anything that runs as a seperate thread.  In other words, if you have to attach to that process to debug it, and you have the source code, the following tip will make it easy to debug.

A Windows service usually runs automatically, it is designed to run as a continuous service and, therefore, spawns one or more threads to do the actual work. This happens very quickly. As such, it is difficult to attach to the process (Alt+P) in time to properly debug the service and attach during a specific part of the execution. As a multi-threaded app, it becomes cumbersome and painful to debug. Fortunately since Visual Studio 2015 that we can vote invoke that allows us to attach to the process through the Visual Studio just-in-time debugger. It’s super simple, call:

System.Diagnostics.Debugger.Launch();

Use this call anywhere in your code where you want the code to break into Visual Studio.  It will invoke a Visual Studio just-in-time debugger prompt:

Visual Studio 2015 Just-In-Time Debugger
Visual Studio 2015 Just-In-Time Debugger

Make your selection and debug.  Happy coding my friends.

For more information: https://msdn.microsoft.com/en-us/library/cktt23yw.aspx

Display QueryString and Form its on Page

I know this probably is one of the most common posts out on the web (for .Net coders), but I can never seem to find it when I need it. So it is here for my reference.

When you are trying to debug an ASP.Net page and you need to see the form collection after a POST or the QueryString (for convenience) for a GET or POST, simply copy the method add it to a page and call it from one of the Page Lifecycle Event Handlers, such as OnInit, OnLoad, etc. I think it is poor practice to hide this output in the body of a page as a comment so I am not doing that (and strongly recommend that you don’t do that either) … simply remove the code or call (if you add it to a library as a static method). I have seen too many people leave the output as commented (html) code … simply bad.

Nonetheless, here is the code snippet:

///
/// Display the QueryString Collection and Form Collection information for a ASP.Net Page
///

private void RenderRequestInfo() {

//Iterate the Request.QueryString collection
Page.Response.Write(string.Format(“ Request.QueryString ({0})
“, Page.Request.QueryString.Count.ToString()));
foreach (string queryItem in Page.Request.QueryString) {
Page.Response.Write(queryItem);
try {
Page.Response.Write(“='” + HttpUtility.HtmlEncode(Page.Request.QueryString[queryItem]) + “‘
“);
} catch (Exception ex) {
Page.Response.Write(string.Format(“=[Error during render.] {0}
“, ex.Message));
}
}
Page.Response.Write(”
“);

//Iterate the Request.Form collection
Page.Response.Write(string.Format(“ Request.Form ({0})
“, Page.Request.Form.Count.ToString()));
foreach (string formItem in Page.Request.Form) {
Page.Response.Write(formItem);
try {
Page.Response.Write(“='” + HttpUtility.HtmlEncode(Page.Request.Form[formItem]) + “‘
“);
} catch (Exception ex) {
Page.Response.Write(string.Format(“=[Error during render.] {0}
“, ex.Message));
}
}
Page.Response.Write(”
“);
}

Cheers!