Sometimes you need to debug an application in a point where it’s difficult to attach debugger manually. For applications which you can’t run directly from Visual Studio (say windows service), you usually use attach process to visual studio to debug. So if you have a windows service running (with debug mode dlls) then you can attach the process to your visual studio to debug. But if your windows service has some code in the constructor that you need to debug then how would you debug the code? When you’ll attach the windows service the constructor code is already executed.
There a way in dot net to prompt to attach a debugger from the running application. If you write the following code in any place in your code then when the application will find this statement the application will prompt you to attach a debugger.
System.Diagnostics.Debugger.Break();
Then if you attach the application with visual studio with code (with correct build version) you’ll find yourself in debug mode. But remember that this statement needs to be removed from code before release build. This is because this statement will be compiled with code even with release build. To make sure you have not put that code accidentally in your release build you can put a preprocessor directive like below:
#if DEBUG<br />
System.Diagnostics.Debugger.Break();<br />
#endif<br />
Sohel has more than six years of experience in professional software development with extensive involvement in Web based Object-Oriented, Multi-Tiered application design and development. He's Familiar with Test Driven Development (TDD) and refactoring techniques as well as having expertise in architecturing large enterprise applications. He has Experience in working with Content Management System and Portal Management System tools like SharePoint, DotNetNuke, Ektron.
Over last few years, he’s involved in development with projects on Microsoft SharePoint and received Microsoft MVP for SharePoint Server Development in the year 2011 and 2012. Currently he's working in a software company located Copenhagen,Denmark on a project integrating SharePoint and SAP. You can read his popular blog at: http://ranaictiu-technicalblog.blogspot.com