Team LiB
Previous Section Next Section

Creating COM + Objects in .NET

Creating COM+ objects using .NET is easy. Follow these steps:

  1. Write the class as shown in the code example following these steps. It is especially important to bring in the following namespaces:

            using System;
            using System.Runtime.InteropServices;
            using System.EnterpriseServices;
            using System.Reflection;
    
  2. Your class must inherit ServicedComponent.

  3. Sign your class using sn.exe, as follows:

    sn.exe --k MyKey.snk
    
  4. Modify the AssemblyInfo.cs file on the following attribute:

    [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
    
  5. Use regsvcs to register the DLL in COM+ as a library application. This means that it runs in the process of the process that instantiates it.

    regsvcs /appname:SRX MyDLL.DLL
    

If you want to register the object as service application, you must open the application with the Component Services snap-in and change the application to a server application using the Activation tab.

The following example demonstrates creating COM+ objects in .NET.

Code Example: Creating COM+ Objects in .NET
Start example

using System;
using System.EnterpriseServices;

namespace Client.Chapter_10___COM_and_.NET_Interoperability
{
          //Allows you to expose an object as a COM object
          public interface IObjCreator
          {
                 void HelloWorld();
          }
          //By inheriting from ServicedComponent, you allow this object to be
          //run in COM+
          public class CObjCreator: ServicedComponent, IObjCreator
          {
                 public void HelloWorld()
                 {
                        //Do something
                 }
          }
}
End example

Team LiB
Previous Section Next Section