Self Hosted Owin WebApi with Structuremap DependencyResolver gotcha

After a lot of debugging I have just solved an issue with an integration test running against an owin selfhosted Web Api. 
The problem i was experiencing was that the code throwing a StackOverflowException when the selfhosted process was disposed:

[Binding]
public class GlobalSetupTearDown
{
    private static IDisposable _webApp;
        
    [BeforeTestRun]
    public static void Setup()
    {
        _webApp = WebApp.Start<StartupSpecs>(Settings.TestPath);
    }

    [AfterTestRun]
    public static void TearDown()
    {
        _webApp.Dispose(); // <-- Throws a StackOverflowException
    }
}

The issue I was experiencing was related to using WebApiContrib.IoC.StructureMap. The actual issue was due to that the Structuremap Container contained an instance to the StructureMapResolver for IHttpControllerActivator. However when StructureMapResolver is being disposed, it will attempt to dispose the container which causes some kind of recursive mess resulting in a StackOverflowException. My solution is not particularly pretty, but seems to solve the problem:

[Binding]
public class GlobalSetupTearDown
{
    // ..snip
    [AfterTestRun]
    public static void TearDown()
    {
        [AfterTestRun]
        public static void TearDown()
        {
            // Manually eject the IHttpControllerActivator (DependencyResolver) to avoid a StackOverflowException
            ObjectFactory.EjectAllInstancesOf<IHttpControllerActivator>();
            _webApp.Dispose();
        }
    }

July 17, 2015