Attaching Visual Studio 2010 as a Silverlight Debugger


Debugging Silverlight 4 applications under Visual Studio is a nice advantage of the tight integration that Microsoft provides with their tooling for rich Internet application development. However, it can be somewhat confusing if you get off the beaten path at all or if you end up with a mis-configured project setting somewhere down the road.

As you might have seen in one of my previous posts, setting up one of the Web applications in your solution as the host project for your Silverlight code is not very difficult.

When the project is started for the first time Visual Studio will prompt you with the following dialog:

Choosing yes to this dialog will enable Silverlight debugging and save the dialog preference in the .user file for the project. Deleting the .user file will cause Visual Studio to prompt you once again for this setting. However, we can manually enable Silverlight debugging very easily on the properties pane for the Web site that is hosting the Silverlight component. The following screen shot shows you where the setting is:

It is worth noting that the tradeoff we make for Silverlight debugging is that Visual Studio will not attach itself as a Javascript debugger as it normally would. If you are having trouble visualizing what that means, consider the next screenshot showing how Visual Studio normally loads the Javascript code up for inspection right in the Solution Explorer:

Attaching Visual Studio as a Silverlight debugger affects System.Diagnostics.Debugger.IsAttached in the running Silverlight code. The default boilerplate code that is generated by Visual Studio uses this to detect how to deal with unhandled exceptions at the top level of the application in App.xaml.cs. If a debugger isn’t attached it attempts to write the exception details out to the browser using Javascript. See the following code snippet:

	private void Application_UnhandledException( object sender, ApplicationUnhandledExceptionEventArgs e ) {
			// If the app is running outside of the debugger then report the exception using
			// the browser's exception mechanism. On IE this will display it a yellow alert 
			// icon in the status bar and Firefox will display a script error.
			if( !System.Diagnostics.Debugger.IsAttached ) {

				// NOTE: This will allow the application to continue running after an exception has been thrown
				// but not handled. 
				// For production applications this error handling should be replaced with something that will 
				// report the error to the website and stop the application.
				e.Handled = true;
				Deployment.Current.Dispatcher.BeginInvoke( delegate { ReportErrorToDOM( e ); } );

Hopefully this give you a little insight on what is going on when you attempt to debug Silverlight code from within Visual Studio and how to get the debugger attached again if something gets messed up.


No Responses Yet to “Attaching Visual Studio 2010 as a Silverlight Debugger”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: