Implementing Serviced Component in COM+ using Visual Studio 2005
page 3 of 7
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 31834/ 54

Building a managed assembly in VB 2005 for COM+ Service

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:

C :\>

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


View Entire Article

User Comments

Title: Good Article   
Name: Jabir
Date: 2011-04-06 8:42:22 AM
Comment:
Still am getting error while browse.......

Error Type:
Microsoft VBScript runtime (0x800A01AD)
ActiveX component can't create object: 'COMPlusObject.COMService'
/COMAppASPClient/CallCOMObject.asp, line 3
Title: Gret Article   
Name: Arthur Knight
Date: 2009-08-13 5:23:30 PM
Comment:
Ggggreeeatttt article, very complete. I achieved my first .net com+ service in less than 10 minutes.

Keep up!!
Title: for transaction   
Name: Abhishek Singh
Date: 2008-02-13 2:16:14 AM
Comment:
I know but i have not done practially. so better try yourselft instead of follwing my assumptions.
Title: How to use Transaction   
Name: Anuj D.Vaijapurkar
Date: 2008-02-12 2:11:11 AM
Comment:
Very Nice! And of Lot of Help!
Please tell me how to handle Transactions from within the ComClass.
Title: Great work   
Name: Sachin
Date: 2008-01-29 1:21:51 AM
Comment:
Great article but I guess COM+ is going to be a thing of the past because of Windows Communication Foundation .
Anyways article was superb.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 7:29:24 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search