Disposing a WCF proxy using an extension method

A while back I read a blog post by Dan Rigsby about why you should not use C# using statements on a WCF service proxy class even though it implements IDisposable. Basically it comes down to the Dispose() method calls Close() which can throw an exception preventing the service proxy from being disposed properly. The solution by Dan Rigsby involving a wrapper class and another solution by Erwyn van der Meer (which provides a helper class that you can derive from) both allow you to use the using statement and still properly dispose of the proxy.

I wrote an extension method below will dispose a service proxy properly, but it doesn't require you to change that proxy in any way. By providing an extension method around ICommunicationObject, any WCF class that dervives from it (ClientBase, ServiceHostBase, etc.) will have this method available as well.

public static class ICommunicationObjectExtensions
{
   public static void TryCloseOrAbort(this ICommunicationObject obj)
   {
      if (obj != null)
      {
         if (obj.State != CommunicationState.Faulted &&
             obj.State != CommunicationState.Closed)
         {
            try { obj.Close(); }
            catch (CommunicationObjectFaultedException) { obj.Abort(); }
            catch (TimeoutException) { obj.Abort(); }
            catch (Exception)
            {
               obj.Abort();
               throw;
            }
         }
         else
            obj.Abort();
      }
   }
}

With that method in place, all you you have to do is call TryCloseOrAbort() in a try-finally block to make sure it is called even if an exception is thrown.

// Example usage - assume MyServiceClient is a WCF service proxy class
var client = new MyServiceClient();

try { client.MyServiceMethod(); }
finally { client.TryCloseOrAbort(); }

Extension method to sort ListItems in a ListControl in ASP.NET

Yesterday I needed a way to sort the items in a ListControl (in this case, a DropDownList) after the list had been populated. The reason for that is after I databound my DropDownList from a collection of items, I inserted a few more ListItems depending on certain circumstances. Instead of trying to hack some method of inserting those items in my original collection, I wanted a more elegant solution. After I came across a post on Google I came up with a solution using an extension method and LINQ:

public static void Sort(this ListItemCollection items)
{
  IList<ListItem> itemList = new List<ListItem>();
  foreach (ListItem item in items)
    itemList.Add(item);

  IEnumerable<ListItem> itemEnum =
    from item in itemList orderby item.Text select item;

  items.Clear();
  items.AddRange(itemEnum.ToArray());
}

//example - assume MyDropDownList is a DropDownList on the aspx page
MyDropDownList.DataSource = SomeMethodThatReturnsAList();
MyDropDownList.DataBind();
MyDropDownList.Items.Insert(0, new ListItem("My Display String", "My List Value"));

//this will sort all the items based on ListItem.Text
MyDropDownList.Items.Sort();

I wanted a simple solution so I didn't do any performance metrics on it. Things I might change is the add ability to sort by ListItem.Value, add the ability to change the sort direction, or perhaps sort by a custom comparer. So if you want a way to sort a ListItemCollection, give this a try and let me know what you think! :)

Using TypeMock to mock nHibernate's ISession.Load<T>

The development team I work with uses nHibernate for our data-access and data persistence needs. We also use Typemock Isolator to mock classes and methods during unit tests. This is especially handy to mock certain nHibernate API calls to make sure our unit tests are more isolated. Today, I needed a way to mock nHibernate's ISession.Load() for all data objects in the project that use nHibernate. To mock a generic method (like ISession.Load()) I would normally do the following:

Mock<ISession> mock = MockManager.MockObject<ISession>(Constructor.Mocked);
mock.AlwaysReturn("Load", new DynamicReturnValue((parameters, context) =>
{
   // assume MyClass has a constructor with int Id parameter
   return new MyClass( (int)parameters[0] );
}), typeof(MyClass));

That means anytime I call ISession.Load(1);, I get back a new MyClass object with 1 passed into the constructor. But what if I have 50 different classes similar to MyClass that I want to mock like this? The simplest way is to iterate over a list of types needed and mock them as well, like below:

Mock<ISession> mockSession = MockManager.MockObject<ISession>(Constructor.Mocked);

// Retrieve all the types in the same assembly as MyClass
// and mock those types that have the "Id" property
Assembly assembly = Assembly.GetAssembly(typeof(MyClass));
foreach (Type type in assembly.GetTypes())
{
   Type objType = type; // this line is needed because 'type' will cause 
                        // issues with the anonymous method below.

   if (objType.GetProperty("Id") != null)
   {
      mockSession.AlwaysReturn("Load", new DynamicReturnValue((parameters, context) =>
      {
         return Activator.CreateInstance(objType, parameters[0]);
      }), objType);
   }
}

Obviously you can change the foreach loop to use whatever list of types you need, but this certainly beats having to duplicate code!

An error was discovered processing the <Security> header

A coworker and I were troubleshooting an issue we were having concerning a .NET call to a webservice that used WSE Security. Our client derived from Microsoft.Web.Services2.WebServicesClientProtocol and every time we tried to call the webservice this error would appear:

An error was discovered processing the <Security> header

A quick search on Google returned a quick, easy solution: verify that client and server have their system time in synch (a 5 minute window is allowed). Once the server time (about 6 minutes apart) was fixed the issue immediately disappeared.

Staging environments via Linode are awesome!

This weekend I wanted to upgrade my Linode server from Ubuntu 7.10 to 8.04 and also to upgrade my Trac server to version 0.11. Now the quickest method would have been to just change the /etc/apt/sources.list file to point to hardy and do a sudo apt-get update; sudo apt-get dist-upgrade and then download the latest version of Trac. But not knowing if my sites would still be running combined with the fact I wasn't quite happy with the way I had set things up originally, I decided to buy another Linode server and set up a staging environment.

Honestly, it wasn't hard at all! Once I installed 8.04 fresh on the stage server, I installed apache, mysql, php, trac, subversion, and other necessary packages, set it up as if it was my primary server (creating aliases like stage.stevenkuhn.net where I could see how it looked), and then copied the new disk image back to the old server. Once there I restarted the old server with the new disk image and since all my original domains still pointed to my production server, I had a very limited downtime. When I was finished, I canceled my second server and received a pro-rated created. I spent less than $2 for having a limited-time staging environment! Linode is the best!

Syndicate content