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
    objAdmin.ShutdownApplication(ConfigurationManager.AppSettings["packageId"]);

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

        COMAdmin.COMAdminCatalogCollection  applicationInstances = 
            (COMAdmin.COMAdminCatalogCollection)
                objAdmin.GetCollection("ApplicationInstances");
        applicationInstances.Populate();

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

        // WWB: Lets Wait Before Checking Again
        if (count>0)
            System.Threading.Thread.Sleep(100);

    }while (count > 0);
}

{6230289B-5BEE-409e-932A-2F01FA407A92}

Comments

Post a Comment

Popular posts from this blog

Yet once more into the breech (of altered programming logic)

How to convert SVG data to a Png Image file Using InkScape

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation