Building a managed component is nothing but developing an
assembly using VS 2005 "Class Library" project. After getting the
error free DLL, we will setup it to make it accessible in COM+ environment. Let
us proceed for building the managed component step-by-step. I have used VB 2005
for this example.
Create a library project to build a valid serviced
component assembly
Open Visual Studio 2005 IDE. Create a class project as:
File >> New >> Projects
Choose Visual Basic as in Project Types and "Class
Library" in project templates.
Give the project name as COMPlusObject and click OK.
It will create the COMPlusObject project containing a
default class Class1.vb. Rename this class to COMService.vb.
In this we need to import System.EnterpriseServices
namespace and before doing it add a reference of System.EnterpriseServices in
the project.
Project >> Add Reference >> .NET >> Choose
System.EnterpriseServices >> OK
Now you can implement following:
Listing 1: Minimum required header files
Imports System.EnterpriseServices
Imports System.Reflection
Now we can implement procedures and functions inside the
class. For this example I will implement only one function named BusinessProcess
as given below. Notice that we are inheriting the ServicedComponent class here.
Listing 2: Class and function implementation
inheriting ServicedComponent
Public Class COMService
Inherits ServicedComponent
''' <summary>
''' Method to process business logic in COM context
''' </summary>
''' <returns>Status Message</returns>
''' <remarks></remarks>
Public Function BusinessProcess() As String
' Required business logice could be implemented here
' This function will be called by the client application
' For this example I will return any normal message
Return "COM Component Processing Completed"
End Function
End Class
Until now, this project is just like a normal library
project implementing a function. Now we actually need to configure it to make
it enabled to be installed as the COM+ component service. After which, we will
install it as a component service in the operating system.
Configuring class and methods of the library project to
build a valid COM+ assembly
Add the following attribute for the class COMService.
Listing 3: Set attributes for the assembly
<Assembly: ApplicationName("MyCOMService")>
<Assembly: AssemblyKeyFile("..\..\bin\ COMPlusObject.snk")>
Notice that for the assembly attribute ApplicationName you
can set any name, but for the AssemblyKeyFile we need to set a strong name key
file location. It gives a shared name which requires making the component
unique. Here we are going to create this key file and place it into the \bin
directory of the project.
To create a key file for the assembly:
Step 1: Open the Visual Studio 2005 Command Prompt and go to
the C: in the command prompt. Now you should be at:
Step 2: Execute sn.exe at the prompt as given below:
C :\> sn.exe –k COMPlusObject.snk
It will show the message:
Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
Key pair written to COMPlusObject.snk
Step 3: Now cut/copy the COMPlusObject.snk file from C: and
paste into the \bin folder of your project directory.
So at moment, the class COMService.vb will have following
complete code:
Listing 4: Complete code for the class in assembly
Imports System.EnterpriseServices
Imports System.Reflection
<Assembly: ApplicationName("MyCOMService")>
<Assembly: AssemblyKeyFile("..\..\bin\COMPlusObject.snk")>
Public Class COMService
''' <summary>
''' Method to process business logic in COM context
''' </summary>
''' <returns>Status Message</returns>
''' <remarks></remarks>
Public Function BusinessProcess() As String
' Required business logice could be implemented here
' This function will be called by the client application
' For this example I will return any normal message
Return " COM Component Processing Completed"
End Function
End Class
One of the important things to configure is to set this
library as COM Visible. Open the properties window of the project.
Right click on the Project >> Click Properties
>> Click Compile tab >> Check the option for Register for COM
interop.
Figure 1: Setting COM Interop option for the
assembly
Now build the project in release mode. After a successful
build you will see three files in the release folder.
Figure 2: Showing files after build in the project
release folder
Now it is the time to setup this assembly in COM+ context.
To achieve we need to do two things.
1. Install the assembly in Global Assembly Cache (GAC).
2. Install the assembly in Component Services list to run as
the COM component.
Install the assembly in GAC
If you want to place the assembly in a shared location GAC
then you can follow following steps.
Step 1: Open the Visual Studio 2005 Command Prompt and go to
the bin directory of project.
C: \COMPlusObject\COMPlusObject\bin>
Step 2: Execute the following command to install the
assembly in GAC.
C: \COMPlusObject\COMPlusObject\bin> gacutil /i Release/COMPlusObject.dll
It will show a message like:
Microsoft (R) .NET Global Assembly Cache Utility. Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
Assembly successfully added to the cache
Figure 3: Installing assembly in GAC using VS
command prompt