U2U Consult TechDays 2011 CD Available for Download

At TechDays 2011 in Antwerp, U2U Consult distributed a CD-ROM with two free tools. I’m happy to announce that the CD-ROM contents is now also available for download from our web site.

The U2U Consult SQL Database Analyzer is a tool for SQL Server database administrators and developers. It displays diagnostic information about a database and its hosting instance that is hard to collect when you only use the standard SQL Server tools. Just point the tool at a SQL Server database of your choice, and have a look at the reports generated by the tool.

The U2U Consult Code Analysis Rules for Visual Studio 2010 are a series of additional code analysis rules for Visual Studio 2010 Premium or Ultimate, and two rule sets with recommended rules for libraries and applications. The rules include additional general performance and design rules, as well as a series of rules specifically for WCF. All rules are documented on the CD-ROM. Obviously, they are applicable to all .NET languages, including C# and VB.

With this CD, for the first time we make two of our own tools available to you. These are only two small components out of the U2U Consult Framework, but we hope they are as useful to you as they are to us and our clients. Enjoy.

Safe disposal of WCF proxies

The issue has been known for a long time: you cannot safely dispose a WCF proxy that inherits from ClientBase<T> with a using statement, because the dispose method may throw.  For more information, see http://www.google.com/search?q=wcf+proxy+dispose.

However, I’ve always found most solutions out there to be too complex, too cumbersome, or both. Some of them even seek to replace the Visual Studio (or svcutil.exe) generated proxy by handwritten code, in which case you loose the convenience of the “Add Service Reference” dialog and the automatic configuration file generation. I wanted a simple but effective solution that does not replace the Visual Studio code generation, and is easy to use at the same time.

I came up with a simple extension method, that you use as follows:

var proxy = new SomeServiceClient();
using (proxy.SafeDisposer())
{
    // use the proxy here
}

Here’s the code:

namespace U2UConsult.ServiceModel
{
    using System;
    using System.Diagnostics;
    using System.ServiceModel;

    public static class ClientBaseExtensions
    {
        private static readonly TraceSource traceSource = new TraceSource("U2UConsult.ServiceModel");

        public static IDisposable SafeDisposer<T>(this ClientBase<T> proxy) where T : class
        {
            return new SafeDisposerProxy<T>(proxy);
        }

        private sealed class SafeDisposerProxy<T> : IDisposable where T : class
        {
            private ClientBase<T> proxy;

            public SafeDisposerProxy(ClientBase<T> proxy)
            {
                this.proxy = proxy;
            }

            public void Dispose()
            {
                if (this.proxy != null)
                {
                    if (this.proxy.State == CommunicationState.Opened)
                    {
                        try
                        {
                            this.proxy.Close();
                        }
                        catch (Exception ex)
                        {
                            ClientBaseExtensions.traceSource.TraceEvent(
                                TraceEventType.Error, 
                                0, 
                                "Could not close client proxy, reason: {0}", 
                                ex.Message);
                            this.proxy.Abort();
                        }
                    }
                    else
                    {
                        this.proxy.Abort();
                    }

                    this.proxy = null;
                }
            }
        }
    }
}

Enjoy!