COM+ Automation Using COMAdminCatalog in .NET C#

Environment: COM+, C#

Accessing COM+ Catalog

The COM+ catalog is the underlying data store that holds all COM+ configuration data. Whenever you do any kind of COM+ administration, you are reading and writing data stored in the catalog. The only way that you can access the catalog is through the Component Services administrative tool or through the COMAdmin library.

Security

To change data on the COM+ catalog, you need to have authority as an administrator. An application using the COMAdmin objects must be running under a user account that is assigned to the Administrator's role on the system application on the machine that it is trying to administer.



Click here for a larger image.

The Applications collection contains an item for each COM+ application that is installed on the machine. The collections occur in a hierarchical structure; the Components collection is subsumed under the Applications collection and holds the components installed into that particular application. In other words, you need to step through several collections to get to the element you want. Each item in a collection exposes properties. These properties serve to hold configuration data for whatever element the item represents. Here are some of the collections that are available in COMAdminCatalog, you can find a complete list of collections on MSDN.

Collections Description
Applications Contains an object for each COM+ application installed on the local machine
Components Holds an object for each component in the application to which it is related
ErrorInfo Retrieves extended error information regarding methods that deal with multiple objects
Roles Holds an object for each role assigned to the application to which it is related
RolesForComponent Holds an object for each role assigned to the component to which the collection is related
RolesForInterface Holds an object for each role assigned to the interface to which the collection is related
RolesForMethod Holds an object for each role assigned to the method to which the collection is related
RolesForPartition Holds an object for each role assigned to the partition to which the collection is related
UsersInRole Holds an object for each user in the role to which the collection is related

Retrieving a COMAdminCatalog Application Collection

//Delegates
public delegate void DeleteEventHandler();
public delegate void ErrorMessageHandler(string s, Exception e);


//Events
/// <summary>Raised when the object is successfully deleted on
/// the remote host.</summary>
public event DeleteEventHandler Delete;
/// <summary>Raised when the object throws an exception.</summary>
public event ErrorMessageHandler Error;

//Method(s) to invoke the event
protected virtual void OnDelete()
{

    if(Delete != null)
    Delete();
}

#region Get COM + Applications
// Get COM + Applications

internal COMAdminCatalogCollection GetCollection()
{
    try
    {
        objAdmin = new COMAdmin.COMAdminCatalog();
        objRoot = objAdmin.Connect("192.168.1.150");

        objCollection =
            (COMAdmin.COMAdminCatalogCollection)
             objAdmin.GetCollection("Applications");
    }
    catch(Exception ex)
    {

        System.Windows.Forms.MessageBox.Show("Error : " + ex);
    }
    return objCollection;
}
    #endregion

Retrieving a COMAdminCatalog Component Collection

#region Get COM+ Applications / Components
// Return all COM + Applications and their corresponding
// components

public NameValueCollection GetCOMApplications()
{
    collection = new NameValueCollection();
    try
    {
        objCollection = GetCollection();
        objCollection.Populate();

        foreach(COMAdmin.COMAdminCatalogObject objAppNames
                in objCollection)
        {
            COMAdmin.ICatalogCollection objComponents = 
            (COMAdmin.ICatalogCollection) objCollection.
             GetCollection("Components",objAppNames.Key);
            objComponents.Populate();
            foreach(COMAdmin.COMAdminCatalogObject Components
                    in objComponents)
            {
                collection.Add(Components.Name.ToString(),
                           objAppNames.Name.ToString());
            } 
        }
    }
    catch(Exception e)
    {
        Error += new ErrorMessageHandler(Message);
        Error("GetCOMAPPlications",e);
        Dispose();
    }
   return collection;
}
#endregion

Deleting an Existing COM+ Application

#region Delete exisiting COM+ Application 
public void DeleteCOMApplication(string appName)
{
    try
    {
        long l_Count = 0;
        ICatalogCollection pCollection = GetCollection();
        ICatalogObject pCatalog;
       pCollection.Populate();
        l_Count = pCollection.Count;
        if(l_Count == 0)
        return;

        for(int i= 0; i < l_Count; i ++)
        {
           pCatalog = (ICatalogObject) pCollection.get_Item(i);
            if(appName == (string) pCollection.get_Value("Name"))
            {
               pCollection.Remove(i);
               pCollection.SaveChanges();
               OnDelete();
               return;
            }
        }
    }
   catch(Exception e)
   {
       Error += new ErrorMessageHandler(Message);
         Error("Unable to delete the COM+ Application:" , e);
         Dispose();
   }
}
#endregion

Creating a New COM+ Application

Create a new COM+ application by adding a new item to the collection. When you set the properties on an item, no changes are actually recorded to the COM+ catalog until you explicitly save the changes. You do this by using the SaveChanges() method on the COMAdminCatalogCollection object for the collection containing the item.

#region CreateCOMApplication
/// <summary>
/// Create a new COM+ application 
/// </summary>
public void CreateCOMApplication()
{
    bool Exists = false;
    try
     {
       Exists = ApplicationExists();    // Method to check whether
                                        // the application exists
                                        // in the Catalog
      if(!Exists)
      {
        objCollection = GetCollection();
        COMAdmin.COMAdminCatalogObject objObject =
        (COMAdmin.COMAdminCatalogObject) objCollection.Add();
        objObject.set_Value("Name",props.COM_Name);
        objObject.set_Value("Description",props.COM_Description);
        objObject.set_Value("Activation",props.COM_Type);
        objObject.set_Value("Identity",props.COM_User);
        objCollection.SaveChanges();
        OnCreate();
      }
      else
      {
        System.Windows.Forms.MessageBox.Show("An application with
               the same name already exixts in the Registry!",
               "Nexsure COM + Help",

      System.Windows.Forms.MessageBoxButtons.OK,System.Windows.
             Forms.MessageBoxIcon.Error);
    }
      }
     catch(Exception ex)
    {
      Error += new ErrorMessageHandler(Message);
      Error("Error creating COM+ Application :" , ex);
      Dispose();
    }
}
#endregion

internal  void Message(string message,Exception e)
{
    System.Windows.Forms.MessageBox.Show("Error occurred " +
                                          message + "
                                          error description : "
                                          + e ,"Nexsure COM + ",
    System.Windows.Forms.MessageBoxButtons.OK,System.Windows.
           Forms.MessageBoxIcon.Error);
}

Starting and Shutting Down COM+ Applications

#region Starting COM+ Application
public  bool StartCOMApplication()
{
      bool Startup = false;
      try
         {
           objCollection = GetCollection();    // Method returns
                                               // COM+ Applications
           objAdmin.StartApplication(applicationName);
           System.Windows.Forms.MessageBox.Show("Application was
                                                 successfully
                                                 started.");
           Startup = true;
          }
         catch
         {
           System.Windows.Forms.MessageBox.Show("Unable to start
                                                 the
                                                 application.");
          }
          return Startup;
}
#endregion

#region Shutting Down  COM+ Application
public  bool ShutDownCOMApplication()
{
    bool ShuttingDown = false;
    try
      {
        objCollection = GetCollection();
        objAdmin.ShutdownApplication(applicationName);
        System.Windows.Forms.MessageBox.Show("Application was
                                              successfully
                                              shut down.");
        ShuttingDown = true;
     }
    catch
    {
      System.Windows.Forms.MessageBox.Show("Unable to shut down
                                            the application.");
    }
     return ShuttingDown;
}
#endregion


Comments

  • proxy

    Posted by A Comment on 12/16/2012 11:07pm

    NameValueCollection is not known

    Reply
  • I have a problem during the complication

    Posted by kristof.pauwels on 07/07/2004 08:58am

    I'm not used to work with C#, i'm a vb.net developper and I need to deploy an existing COM+ component. This code fragment is just what I need, but I have a compile problem.
    
    NameValueCollection is not known, what COM object do I need to include in order to function corretly?

    Reply
  • proxy

    Posted by Legacy on 07/25/2003 12:00am

    Originally posted by: Dmitry

    I have to add application proxy in COM+ catalog programmatically, not using export in .MSI from Component Service Manager. Problem is that "ApplicationProxy" property is ReadOnly.

    Reply
  • How to create proxy?

    Posted by Legacy on 07/23/2003 12:00am

    Originally posted by: Dmitry

    How to create proxy application programmatically?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds