Total Pageviews

6 Aug 2013

System.Diagnostics.Debugger.Break() for debugging Features

In certain cases, there is no easy way to debug FeatureActivating or FeatureDeactivating methods either because there is no UI process to attach to or you are deploying the project from Visual Studio. For example, If you set a breakpoint inside the asynchronous FeatureActivating method - it will not hit during execution unless you are somehow attached to the correct process.

So, for debugging purposes, you can include System.Diagnostics.Debugger.Break();  inside the FeatureActivating method:

        public override void FeatureActivating(SPFeatureReceiverProperties properties)
        {
             System.Diagnostics.Debugger.Break();       

             // Feature code...
        }

At the time System.Diagnostics.Debugger.Break() is executed you System will show you a dialog box with an exception, prompting you to select a desired way to debug "a problem". It's not a problem of course :)  Then you have to select your Visual Studio SharePoint project which should be already open. After a few moments you will be redirected to the System.Diagnostics.Debugger.Break(); and you will see that your lovely breakpoint actually worked fine. Now you can go on and continue debugging.

No comments:

Post a Comment