Archive for the ‘programming’ Category
Only Catching Exceptions When Debugger Isn’t Attached
When building applications that will run unattended (for example from a scheduled task or a Windows Service) you usually need to be quite aggressive when catching exceptions. This can be quite detrimental to the debugging experience and there are two common workarounds to this problem:
Configuring Visual Studio to Break When Exceptions Are Thrown
By default Visual Studio will break when exceptions are unhandled, but if you have aggressive exception handling then most exception will be handled and Visual Studio will never break. You can configure Visual Studio to break when an exception is thrown rather than only if it’s unhandled from the Debug | Exceptions menu. This shows the dialog below which allows you to choose for each exception type under which circumstances Visual Studio will break.

Adding Breakpoints Manually
The alternative is to manually add breakpoints in the exception handlers you’re interested in debugging as shown here:
But Wait… There Is Another Option
Visual Basic allows you to add a filter to your exception handlers specifying a condition under which the exception handler will be in effect.
We can leverage this to make our handler only catch the exception if there isn’t a debugger attached which means if a debugger is attached the exception will be unhandled and the debugger will automatically break.
We use the shared IsAttached method of the Debugger class in the System.Diagnostics namespace to detect if a debugger has been attached as shown in this example:
Try teamFoundationServer.EnsureAuthenticated() Catch ex As TeamFoundationServerException When Not Debugger.IsAttached My.Application.Log.WriteException(ex) Environment.Exit(ExitCode.TeamFoundationServerExceptionAuthenticating) Catch ex As WebException When Not Debugger.IsAttached My.Application.Log.WriteException(ex) Environment.Exit(ExitCode.WebExceptionAuthenticating) End Try
Setting Default Values for Workflow Dependency Properties
When setting default values for dependency properties in Workflow Foundation there are two steps:
- Pass a PropertyMetadata object containing the default value to the DependencyProperty constructor (Line 8). This defaults the value of the property to the passed value.
- Apply the DefaultValue attribute to the property (Line 14). This provides the default value to the PropertyGrid which allows functionality such as the property appearing bold if the value is different to the default and the right-click Reset option to set the property back to the default.
For example:
1: Private Const DefaultPort As Integer = 25 2: 3: Public Shared PortProperty As DependencyProperty = _ 4: DependencyProperty.Register( _ 5: "Port", _ 6: GetType(Integer), _ 7: GetType(SendEmail), _ 8: New PropertyMetadata(DefaultPort) _ 9: ) 10: 11: <Description("Port")> _ 12: <Category("Input")> _ 13: <Browsable(True)> _ 14: <DefaultValue(DefaultPort)> _ 15: <DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _ 16: Public Property Port() As Integer 17: Get 18: Return (CType((MyBase.GetValue(SendEmail.PortProperty)), Integer)) 19: End Get 20: Set(ByVal Value As Integer) 21: MyBase.SetValue(SendEmail.PortProperty, Value) 22: End Set 23: End Property
Workflow Tracking Events Not Available Until Workflow Completes
I was using Workflow Foundation as part of an internal process recently and I couldn’t work out why the tracking events generated by the SqlTrackingService weren’t being written to the tracking database until the workflow completed or was terminated.
The issue was that the SqlTrackingService is transactional by default and the transaction isn’t committed until the workflow completes or is terminated. This can be changed by setting the IsTransactional property to False before adding the service to the WorkflowRuntime’s services collection.
http://msmvps.com/blogs/theproblemsolver/archive/2006/09/28/Tracking-in-Workflow-Foundation.aspx
Stop Visual Studio 2008 from Running Built-In WCF Service Host
I was working on a project recently where I had a solution with a project that self-hosted a WCF service and another project containing the service itself.
When running the solution with self-hosted project as the startup Visual Studio would also run the built-in WCF service host resulting in a clash for URLs/ports.
The solution is in the WCF Options tab in the service’s project properties. If you clear the Start WCF Service Host When Debugging Another Project In The Same Solution checkbox Visual Studio won’t run the built-in WCF service host unless the service project is the start-up project.
Calculating Total Size of a Directory Using LINQ
Dim totalSize As Decimal = (Aggregate file In New System.IO.DirectoryInfo(“c:\dev\”).GetFiles(“*.*”, IO.SearchOption.AllDirectories) Into Sum(file.Length)) / 1024 / 1024
Schemas for Work Item Type Definitions
I’m doing a lot of work with Work Item Type Definitions and for those that don’t know the Visual Studio SDK includes schemas for these that can be used in Visual Studio (and other XML editors) to provide Intellisense capabilities.
Rob Caron blogs about this here. For those that don’t want to download the whole SDK he also provides a direct download of just the schemas.
Cannot Remove Reference from Web Site Project in Visual Studio 2008
I just ran into an issue where I right-clicked a Web Site Project in Visual Studio 2008 and chose Property Pages and then tried to remove a reference. However, whenever I clicked the Remove button the dialog just flashed and the reference remained.
The issue turned out to be that the web.config file (where the reference information is stored) was read-only. Making the file read-write again solved the issue.
TFS/VSTS "Rosario" Feature Requests
I’ve raised a number of feature requests on Connect for the “Rosario” release of TFS/VSTS. If you think any of these are valuable please vote for them:
Making TextPad Standard
We have a site license for TextPad but I always find myself lusting after other editors (such as Notepad++).
I reinstalled TextPad today and instantly remembered the one thing that annoys me most about it… Ctrl+F is not Find, F5 is…
Luckily like most applications it does however allow changing key bindings, so I made the following changes and I’ll see how I go:
SearchFind – Ctrl+F
SearchFindNext – F3
FileManage – F5
Introduction to Work Item Customisation
I’ve uploaded my Introduction to Work Item Customisation presentation to SlideShare. I originally presented this at the Queensland VSTS Users Group.
