<%@ 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.
Namespaces involved
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.
Imports System.EnterpriseServices
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
<Assembly: ApplicationName("EmployeeApp")>
<Assembly: ApplicationActivation(ActivationOption.Library)>
<Transaction(TransactionOption.Required),
ObjectPooling(MinPoolSize:=2,
MaxPoolSize:=5,
CreationTimeout:=20000)>
Public Class Employee
Inherits ServicedComponent
Public Sub AddEmployee
(ByVal lname As String, ByVal fname As String)
Try
Dim cnn As OleDbConnection
Dim cmd As OleDbCommand
cnn = New OleDbConnection("Provider=SQLOLEDB.1;
User ID=sa;Initial Catalog=Northwind;
Data Source=WIN2000\netsdk")
cnn.Open()
cmd = New OleDbCommand
("insert into employees(lastname,firstname)
values('" & lname & "','" & fname & "')", cnn)
cmd.ExecuteNonQuery()
ContextUtil.SetComplete()
LogMsg("Employee added...")
Catch ex As Exception
ContextUtil.SetAbort()
LogMsg("Employee addition failed...")
End Try
End Sub
<AutoComplete()> Public Sub CalculateBonus()
End Sub
Public Overrides Sub Activate()
LogMsg("Object Activated...")
End Sub
Public Overrides Sub Deactivate()
LogMsg("Object Deactivated...")
End Sub
Public Overrides Function CanBePooled() As Boolean
LogMsg("Can be pooled called...")
Return True
End Function
Private Sub LogMsg(ByVal msg As String)
Dim writer As StreamWriter
writer = System.IO.File.AppendText("mylog.txt")
writer.WriteLine(msg)
writer.Close()
End Sub
End Class
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.