.netCoders Contact Us
Search:

Serviced Components

Microsoft has not left Component Services behind. In case you are not familiar with the term, Component Services, which was previously called MTS, refers to a set of plumbing services available to components. Services include object pooling, transaction processing, and just-in-time activation. You can access the Component Services management console from the Start menu by going to Programs->AdministrativeTools->Component Services.

Implementing a Serviced Component

Implementing a .NET Component that takes advantage of Component Services, a "Serviced Component", involves the following steps:
  • Derive a class from the ServicedComponent class
  • Apply service attributes
  • Compile the assembly with a strong name
  • Register the assembly
Let's work through an example. The following code shows a serviced component. Notice the ServicedComponent base class, and the service attributes indicating the transaction support. We also make use of the AssemblyKeyFileAttribute to indicate the file with the strong name for the assembly.
using System.EnterpriseServices;
using System.Reflection;

[assembly: AssemblyKeyFileAttribute("ServComp.snk")]

namespace DotnetCoders
{
    [Transaction(TransactionOption.Required)]
    public class TransClass : ServicedComponent
    {
        public void TransferMoney(string strToAccount, string strFromAccount, double dAmount)
        {

            //Do work to transfer money

            ContextUtil.SetComplete();
        }
    }

}
No special code is required for clients using a ServicedComponent. The following client application makes use of the ServicedComponent above.
using DotnetCoders;

class ServClient
{
    public static void Main()
    {
        TransClass tc = new TransClass();
        tc.TransferMoney("abc", "xyz", 100.00);
    }
}
The following batch file generates a strong name key matching the name used in the AssemblyKeyFileAttribute attribute in our ServComp.cs file. When we compile the component, the strong name is compiled into the assembly.
sn -k ServComp.snk
csc /t:library /r:System.EnterpriseServices.dll ServComp.cs
csc /t:exe /r:ServComp.dll ServClient.cs
pause
Run the client application. Because we didn't code any functionality, nothing will seem to happen. Behind the scenes though, we have taken advantage of dynamic registration. If a serviced component has not been registered when first called, the .NET runtime automatically registers the assembly and creates a COM+ application. If you look in the Component Services management console, you'll see that a new COM+ application has been created called ServComp containing the TransClass.

Attributes are using within .NET to indicate which component services a class uses. We have already seen the Transaction attribute, which indicates that a .NET Component makes use of the Transaction Processing services. Other attributes include the following (table from .NET Framework SDK):

Attribute Attribute scope Description
ApplicationActivation Assembly Specifies whether components in the assembly run in the creator's process (Library) or in a system process (Server).
ApplicationID Assembly Specifies the application GUID for this assembly.
ApplicationName Assembly Specifies the name of the COM+ application to be used when registering the assembly.
ApplicationQueuing Assembly Enables queuing support for the assembly.
AutoComplete Method Indicates that ContextUtil.SetComplete should automatically be called if the method call returns normally.
ComponentAccessControl Class Enables security checking on calls to a component.
ConstructionEnabled Class Enables COM+ object construction support.
Description Assembly, Class, Method, Interface Sets the description of the item
EventClass Class Marks the class as an event class for the COM+ Loosely Coupled Events service
JustInTimeActivation Class Turns just-in-time (JIT) activation on or off
MustRunInClientContext Class Forces the attributed object to be created in the context of the creator
ObjectPooling Class Enables and configures object pooling for a component.
PrivateComponent Class Identifies a component as a private component that is only seen and activated by components in the same application
SecureMethod Assembly, Class, Method Ensures that the infrastructure calls through an interface for a method when using the security service
SecurityRole Assembly, Class, Interface Configures a role for an application or component
Transaction Class Specifies the type of transaction support, from the TransactionOperation enum

Strong Named Assemblies

When assemblies are installed in the Global Assembly Cache (see next section), or use Component Services, they must be signed with a strong name. This process has two steps:
  1. Generate a Key using the Strong Name Utility (sn.exe)
  2. Signing the Assembly with the Key
Generating Keys
The Strong Name Utility (sn.exe) is a command-line utility for producing public/private key pairs, which are stored in .snk files. To generate a new key pair, using the -k switch. For example, the following command instructs the Strong Name Utility to generate a new key pair in the file newkey.snk.
sn -k newkey.snk

Signing the Assembly
There are two ways to sign an assembly, via Attributes within the code and during compilation, or via the Assembly Linker (al.exe) tool.

To sign using the Assembly Linker, you must have an assembly that does not already have an assembly manifest. This is referred to as a module. The syntax for the assembly linker utility is:

al /out:<assembly name> <module name> /keyfile:<file name>

To sign using attributes, include the AssemblyKeyFileAttribute within a class, such as the following:

[assembly:AssemblyKeyFileAttribute("newkey.snk")]

Global Assembly Cache

The Global Assembly Cache is a machine-wide cache. The GAC, as it's called, houses assemblies that are designed to be shared amongst several applications. There are several ways to install an assembly into the GAC:
  • Global Assembly Cache Utility (gacutil.exe)
  • Windows Explorer Folder Extension
Before an assembly can be installed in the GAC, it must be signed with a strong name using the Strong Name Utility (sn.exe).

The Global Assembly Cache Utility
The gacutil exe is a command-line utility used to list, install, and uninstall assemblies in the global assemble cache. To see all the options of the utility, open a command window and type

gacutil /?
To list assemblies installed in the global assembly cache, use the /l switch:
gacutil /l
To install assemblies into the global assembly cache, use the /i switch along with the path of the assembly containing the manifest:
gacutil /i mySharedDLL.dll
To uninstall an assembly from the global assembly cache, use the /u switch along with the name of the assembly without the .dll extension:
gacutil /u mySharedDLL

GAC Explorer
If you navigate to the Windows directory, you'll notice a directory called assembly . This is no ordinary directory. It has a special interface that allows you to manage components within the Global Assembly Cache.

Component Services Tool

The Component Services tool is an administrative console for managing components registered to take advantage of component services. This tool can be found under Administrative Tools, either from the Programs Menu or the Control Panel. Components are grouped into COM+ Applications. New applications can be created by right-clicking the COM+ Applications folder andselecting New->Application. Components can be added to an Application by either dragging and dropping the dll, or by right-clicking the Components folder under the application and selectingNew->Component. This will launch the Component Install Wizard. The following screenshot shows the Component Services management console from a Windows .NET Server machine.

Additional Resources

How Do I...Work with the global assembly cache?