IDisposable, WCF, and Hole In The Middle

Steve Smith recently posted about the problem of the Close() (or Dispose()) method throwing an exception in WCF.  He proposed a solution that was an extension method to the ICommunicationObject interface and handled safely closing the connection.  Now, if you read my blog you likely know I love Extension Methods, and even more so love them on Interfaces, but his code resulted in a usage like this: var myClient = new ServiceClient(); try { int someValue = myClient.GetSomeValue(); } finally { myClient.CloseConnection(); // extension method }   This is ok, but as he noted doesn’t actually save you much over a using() block except that the error is not thrown from a closing brace, which is confusing.  But if you look at this code, it is a classic example of a Hole In The Middle.  We want to do some common setup, and common tear down, but do something different in the middle.  The easiest way to get around Hole In The Middle problems in C# is to use lambdas.  But of course we want to do this in a way that is easy, and preserves our type safety. So I sat down with Visual Studio, and after about 15 minutes came up with this: public static TResult SafeExecution<TExtension, TResult> (this TExtension myServiceClient, Func<TExtension, TResult> serviceAction) where TExtension : ICommunicationObject { TResult outValue;   try { outValue = serviceAction.Invoke(myServiceClient); } finally { myServiceClient.CloseConnection(); }   return outValue; } This solution uses the CloseConnection extension method Steve created, but results in a usage that looks like this: var ec2 = new Amazon.AmazonEC2PortTypeClient(); ec2.SafeExecution(i => i.AllocateAddress(null)); As you can see, this is a more compact, but still type-safe way of addressing this problem and does so with an Extension Method so you can re-use this code in any project you encounter. This is a new solution to me, so I’m certainly open to comments, but I think it is an elegant solution.  What do you think?

Dallas TechFest 2009 - Postponed

UPDATE : Dallas TechFest has been scheduled and the website can be found at http://DallasTechFest.com Last year TimRayburn.net LLC was formed to take on the project of putting on Dallas TechFest 2008, with the full intention of putting then on for years to come.  My original plans had been to put on the next Dallas TechFest during March of this year, but given several things, some avoidable some not, that is not going to be workable.  The harshest reality is that with the current economy, finding sponsors willing to support the event is much harder.  I have high hopes that a 2009 event may still be doable, but it will not be doable in March.

INETA Speaker's Bureau

What a way to kick off the new year!  I've just been notified (ok, it happened a few days ago but I was on vacation) that I've been selected to join the INETA NORAM Speaker's Bureau.  Stupidly happy is a massive understatement, I am a huge fan of INETA's work getting great speakers to user groups throughout the United States and Canada, and I couldn't be more happy to join the ranks of speakers who will be delivering interesting, cutting edge content to user groups. When I informed my employer of this honor this morning, I used the analogy that I consider this the equivalent of being called up to the majors in baseball.  It is an opportunity to shine, but it also means that I'm the rookie now no matter what I've done before. I will let everyone know once I'm in the system enough to be requested at your local user group.  Also congratulation to all those who were selected at the same time I was, particularly my pal and fellow Connected Systems MVP Stuart Celarier.