Registering Serviced Components

Before you get into the meat of writing code that takes advantage of the many services available through COM+, you need to be aware of the most basic of all COM+ tasks: registration. In order for a component to take part in COM+ transactions, be JIT-activated, exist within a pool, and so on, that component must be registered with COM+. You can either manually register your components or you can make use of special assembly-wide code attributes to automatically register the components. In order to test any of the code written in the remainder of this chapter, you will have to register the components.

Manual Registration

Manual registration of a serviced component involves using the regsvcs.exe command-line tool. This tool ships with the .NET Framework. For a complete reference on this tool, check out the MSDN documentation for it at

Before you can register your components, the assembly in which they reside needs to have a strong name. Strong-naming an assembly is as simple as checking the appropriate box in the Project Settings area.

To register all public classes within an assembly as serviced components within a COM+ application (a COM+ application is a collection of components that share similar properties and configuration), you can use the following syntax:

regsvcs /appname:TestApp MyComponents.dll 

The appname parameter refers to the name of the COM+ application. If you are unfamiliar with the basic features of COM+ and how it works, consult the administrator's guide for your operating system.

Automatic Registration

Automatic registration can be much simpler than manually registering your components. Through the use of a special set of code attributes, you can embed COM+ information into your assembly. The first time the assembly is loaded and these attributes are found, the components contained in your assembly can be registered automatically before the first line of component code is executed. Table 40.1 contains a list of the attributes involved in automatic registration as well as the configuration that can take place during that configuration.

Table 40.1. Enterprise Services Assembly-Wide Attributes




This attribute is used to indicate whether the COM+ application is activated in the client process (Library mode) or in the server host process (Server mode).


This is used to provide a GUID for the COM+ application. If this attribute is left out, one will be automatically generated.


Indicates the name of the COM+ application. Specifying this attribute will allow for automatic registration upon the invocation of a serviced component within the assembly.


Configures queued components.


In order for the components in this library to be registered with COM+, the ComVisible attribute must be set to true. Visual Studio defaults this attribute to false.

To see how automatic registration works, and to create the first serviced component in this chapter, start by creating a new console application called ComPlusHelloWorld. Add a new Class Library project to the solution called HelloLib.

In the class library, add a reference to the System.EnterpriseServices Assembly, delete Class1.cs, and add a new class called HelloWorldComponent.cs. The code for this class is shown in Listing 40.1.

Listing 40.1. HelloWorldComponent

using System; using System.Collections.Generic; using System.Text; using System.EnterpriseServices; namespace HelloLib { public class HelloWorldComponent : ServicedComponent {     Public HelloWorldComponent()     {         // all COM+ components MUST         // 1. be public         // 2. be concrete (generics)         // 3. have public constructor     }     public void HelloWorld()     {         Console.WriteLine("Hello world from within a COM+ component!");     } } } 

Add a strong name to this assembly and make sure that the ComVisible attribute in the AssemblyInfo.cs file is set to false. Also, add the following attributes to the AssemblyInfo.cs file:

[assembly: ApplicationActivation(ActivationOption.Library)] [assembly: ApplicationName("Hello World Application")] 

Now add a reference to the HelloLib project and the System.EnterpriseServices assembly from the ComPlusHelloWorld console application project. Modify Program.cs in the Console Application to contain the following code:

using System; using System.Collections.Generic; using System.Text; namespace ComPlusHelloWorld { class Program {     static void Main(string[] args)     {         HelloLib.HelloWorldComponent hello =             new HelloLib.HelloWorldComponent();         hello.HelloWorld();         Console.ReadLine();     } } } 

Note that nothing special needs to be done to create an instance of a serviced component other than adding a reference to the component's assembly.

Run the console application and you will see the "Hello World" message from the component. Then go into the Component Services administrative control panel to verify that a COM+ application was indeed created for your assembly. Figure 40.1 shows the Component Services control panel showing the new COM+ application.

Figure 40.1. An automatically registered COM+ application.

Now that you know the mechanics of creating a serviced component, registering it, and utilizing it, you can continue through this chapter and learn how to make use of the various COM+ services.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: