<%@ Page %>
Creating COM+ Aware Components in .NET
Why host .NET components in COM+
COM+ provides many �middle ware� services like increased scalability, Transactions, object pooling, Just-In-Time activation to your components. Enterprise components typically developed in VC++ or VB are often hosted in COM+ to gain above advantages. As you might be aware that .NET components are different than traditional COM components in terms of reference counting, memory management and registration. However, .NET provides a way to host your .NET components inside COM+ environment. In fact it provides easy programmatic configuration options that were lacking in traditional VB or VC++. In this article we will see with an example how to use .NET components with COM+. This article assumes that you are using Visual Studio.NET for your development work. I assume that you are already familiar with MTS/COM+ fundamentals.
All the functionality you need to write a COM+ aware component in .NET can be found in System.EnterpriseServices namespace. To indicate to the .NET runtime that your class is COM+ aware, your class must inherit from ServicedComponent class.
Creating your class
Create a new project using VS.NET. For this example I will be using VB.NET but you can choose language of your choice. Add a class to the project with name Employee (Be sure to take care of line breaks). Following is the complete code for the class. We will dissect the code soon.
Public Class Employee
Public Sub AddEmployee
(ByVal lname As String, ByVal fname As String)
Dim cnn As OleDbConnection
Dim cmd As OleDbCommand
cnn = New OleDbConnection("Provider=SQLOLEDB.1;
User ID=sa;Initial Catalog=Northwind;
cmd = New OleDbCommand
("insert into employees(lastname,firstname)
values('" & lname & "','" & fname & "')", cnn)
Catch ex As Exception
LogMsg("Employee addition failed...")
<AutoComplete()> Public Sub CalculateBonus()
Public Overrides Sub Activate()
Public Overrides Sub Deactivate()
Public Overrides Function CanBePooled() As Boolean
LogMsg("Can be pooled called...")
Private Sub LogMsg(ByVal msg As String)
Dim writer As StreamWriter
writer = System.IO.File.AppendText("mylog.txt")
Let us dissect the code :
Compiling the component
Before you compile your class you need to sign your assembly with a strong name. In VS.NET IDE you can go to project properties dialog and do that. Signing an assembly with a key file ensures its uniqueness on a given machine.
Creating client for your component
For the purpose of testing you can create any kind of client application like Console application, WinForm application or even ASP.NET application. Then as with any other component you can create instances of the Employee class. Note here that unlike traditional MTS/COM+ development we have not 'manually' registered our component inside the component explorer. Run the client application and then check the log file "mylog.txt" examine sequence of method calls.