XP, Component Services and .NET

More on .NET and COM+

Apply Attributes

Practically all technology uses the term "attributes," that confuses programmers and users. Certainly, an attribute is a property of an item that gives information about the item, its performance, or its requirements. C++ attributes in Visual Studio .NET are used to write four kinds of code: OLE DB consumer code, performance counter code, Web Services and ATL Server code, and COM objects. In this article, I will restrict myself to the Visual C++ COM attributes that are used to define the behavior of COM components. The concept of C++ attributes is quite simple.

An attribute is applied to a class or function by declaring it before the code, using square brackets in the same style as IDL. I have deliberately chosen this attribute as an example because it has the same name as the IDL statement that it replaces. This declaration is in a header or CPP file, and the idea is that the code which specifies that the server DLL has an implementation of a coclass called CManager has been moved out of the IDL file and put in the implementation files. This makes perfect sense because the coclass statement in a type library is about implementation; it describes a component that implements one or more interfaces.

Components using COM+ services are suggested to specify an assembly header with attributes. Either an assembly name is obtained from the metadata, from the assembly's FullName, or the value obtained from an ApplicationName attribute. If the assembly is tagged with an ApplicationID or Guid attribute, all searches for the application are based on that guid, not on the application name. Additional, component configuration can applied at design time, through attributes

If a class in .NET works with COM+ services then it is need to be registered in a COM+ application in the COM+ catalog as a usual COM+ component. When an instance of the class is created within .NET, this registration is done automatically based on the attributes in the assembly.

However, if a new instance of the class is required from an unmanaged environment, then the assembly must be registered manually. This can be achieved using the tool, regsvcs. When writing COM+ components, think how you would adjust components in the Component Services. What is the COM+ application name? Is the application activation type, server, or library? Are my objects pooled? Do I use a construction string?

What is a .NET Component?

Components are defined in various ways. However, some general observations may be made. Components are interchangeable software parts that are both an artifact of "industrialized" systems and the impetus for their success. In the Component layer of the .NET Platform, components are created as Assemblies for interoping by means of Metadata "Blueprints" as applications and with .NET basic system layers.

Basically the .NET Platform creates and works with components as its fundamental building block. A .NET Platform component is basically an interchangeable software part, built in any .NET language with metadata "blueprints" for assembly, independently deployable in a plug-in fashion and ready to interop with other programs. A .NET component that uses COM+ services is called a serviced component to distinguish it from the standard managed components in .NET.

Strongly Named .NET Components

Now, we will learn what the strong name assembly (.NET Components) is. When a configured class is developed, it must be compiled. After compiling the code, there are two things to consider. Primarily, the COM+ integration needs that the compiled assembly be strongly named. You must generate a key by running the Strong Name Utility named sn.exe to create a strongly named assembly. Once you compile your strong named assembly you must reference the key, which is stored in a file, from within your component's code using an assembly-level attribute called AssemblyKeyFileAttribute from the System.Reflection namespace.

#using <mscorlib.dll>
using System;
using System::EnterpriseServices;
using System::Reflection;

[assembly: ApplicationName("FirstApp")]
[assembly: ApplicationActivation(ActivationOption.Library)]

// AssemblyKeyFile attribute references keyfile generated
// by sn.exe - assembly will have strong name
[assembly: AssemblyKeyFile("thiskeyfile")]

namespace ESExample
{
    ...
}

Second, when you compile your strongly named assembly, you must reference the assembly that exports the types in the System.EnterpriseServices namespace, System.EnterpriseServices.dll. Here are the commands for generating a key and compiling a configured class:

sn -k thiskeyfile
Cl /out:ThisExample.dll /t:library
/r:System.EnterpriseServices.dll FirstCfgClass.cpp

You might also like...

Comments

About the author

John Godel United States

John H. GODEL has an experience more than 22 years in the area of software development. He is a software engineer and architect. His interests include object-oriented and distributed computin...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout