Registering Classes with COM

Your C# classes that are designed for use in a COM+ application must follow the same basic rules as C# classes that are designed for use by classic COM clients. Chapter 34 describes how C# is used to build COM components. Like COM components written in C#, COM+ components written in C# must be compiled into a DLL-based assembly, and must have a strong name (which requires that the assembly have a public-key pair and version information). Like COM components, this information can be specified for COM+ components through attributes specified in your C# source code.

You can install your classes into a COM+ application using a command-line tool called regsvcs that ships with the .NET Framework. This command-line tool registers all public classes found in a DLL-based assembly with COM+, and performs all the registration necessary to make the classes visible as COM+ classes.

Listing 35-2 is a slight modification to Listing 35-1. It contains the attributes necessary to prepare the generated assembly to support a strong name.

Listing 35-2: Poolable COM+ Object with Strong Name Attributes using System.Reflection;

using System.EnterpriseServices;

[assembly:AssemblyKeyFile("keyfile.snk")] [assembly:AssemblyVersion("1.0.*")] [ObjectPooling(5, 10)]

public class PooledClass : ServicedComponent {

public PooledClass()

PooledClass()

public override bool CanBePooled() return true;

public override void Activate()

public override void Deactivate()

You can expose this class as a COM+ class with just a few command-line tools. First, generate a new key pair for the strong name of the assembly with the standard sn command-line tool:

sn -k keyfile.snk

Then compile the code into a DLL-based assembly:

csc /target:library Listing35-2.cs

After the assembly is generated, you can use the regsvcs tool to register the assembly with COM+:

regsvcs /appname:Listing2 8-2App Listing35-2.dll

Tip The .NET/COM+ interop infrastructure supports COM+ applications based on assemblies in the global assembly cache. If multiple clients use your code, you might want to install your assembly in the global assembly cache before registering it with COM+.

The /appname argument to the regsvcs tool specifies the COM+ application name created to house the public classes found in the assembly. If a COM+ application already exists with the given name when regsvcs runs, the classes are added to the preexisting application.

Figure 35-1 shows the COM+ Explorer running with the assembly generated from the code in Listing 35-2 registered with COM+. The PooledClass is automatically detected by the registration process and added to the COM+ application.

GmmIé Widow H»lp □ Ü B

03 -.*. »!

--.■ry y» ■)" S "S il Ö 'j 1" !J; Hl

•w 1 'ww 1

1 ' "I

Lc-rf-irWi Fl=«:

9 ¿1 PWWÖHI < URoh» 11 HTM i tiriM 1 iiw n**iaam Ï 4 tWh um»

H <tEUKu

- 4 l^ifflTiT

I I I

Figure 35-1: COM+ Explorer with a registered .NET assembly

Figure 35-1: COM+ Explorer with a registered .NET assembly

Launch the COM+ Explorer by performing the following steps:

1. Click the Windows Explorer Start button. The Start menu appears.

2. Choose Programs ^ Administrative Tools. The icons for applications in the Administrative Tools program group appear.

3. Select Component Services. The COM+ Explorer appears.

Figure 35-2 shows the COM+ property sheet for the PooledClass class. Note that the object pooling information specified in the attributes in Listing 35-2 are automatically detected by the registration process and added to the COM+ application.

Pdq Ic-dC-lass. Propnrtie

| TI^IIJUMK I SMU^ ictjvvn- | CincuvíiKíl flí^mídj

P t1"^1 QtiHtpaSkQ UkmH^

T~ frtftblo Juxl In liv-Ailiv^tii I iHud bp «Ir^JM ri cífei'i oiotat!

0 0

Post a comment