Tuesday, November 25, 2008

Shutting Down a Running COM+ Instance

If you have an instance of object running under COM+ (in the dllhost.exe process space) and you want to shut it down from C#, you can use the code below.  Note that ShutdownApplication only intializes the shutdown of the COM+ applicance, it will return sometimes before the application is finished shuting down.  So you need to check to make sure the applicatio is shut down before continue.  I use this code in my unit testing to kill the instance-- making sure that it is stateless.  Also note that you need to send the package id for the application -- this is the guid you used when installing the application into COM+.  It can be found on the first property page of the application properties in component manager.

internal static void ShutDown()
    Int32 count = 0;
    COMAdmin.COMAdminCatalog objAdmin = new COMAdmin.COMAdminCatalog();

    // WWB: ShutDown The Application (All Instances) Using teh Package Id

    // WWB: Iterate Over All Instances Making Sure They Are Shutdown
        count = 0;

        COMAdmin.COMAdminCatalogCollection  applicationInstances = 

        foreach (COMAdmin.COMAdminCatalogObject applicationInstance in applicationInstances)
            if (new Guid(applicationInstance.Name.ToString()) ==
                new Guid(ConfigurationManager.AppSettings["packageId"]))

        // WWB: Lets Wait Before Checking Again
        if (count>0)

    }while (count > 0);


1 comment: