AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1543&pId=-1
Implementing Serviced Component in COM+ using Visual Studio 2005
page
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 31967/ 65

Introduction

COM+ is a programming model based on Component Object Model (COM). COM+ does not replace the COM. In fact, COM+ technology uses the COM by extending its features to provide enterprise level services.

COM was the very first methodologies to facilitate object oriented software implementation. It is mainly used to encapsulate business task and logic into one business object, called COM component in the distributed environment which can be shared and accessed by different applications.

In the real world scenario, more than one COM component work altogether to achieve a high density task. To manage the integrity and security among different components, COM uses another service of Microsoft, called Microsoft Transaction Server (MTS).

In other words, COM+ is nothing but a combination of both COM and MTS. It means COM+ = COM + MTS. COM+ was first introduced in Microsoft Windows 2000 Server. COM+ also includes additional features like object pooling, load balancing, component queuing, etc. These services were being known as the COM+ Component Services. When multiple components are grouped together it is referred to as COM+ applications.

COM+ Component Service using .NET

Microsoft Visual Studio has come up with the libraries and classes through which we can create, install and consume COM components. In fact, .NET has been designed to offer COM+ services through these and becomes easier. Once COM+ component is installed correctly it can be used by a variety of applications (including web applications), and these applications are termed as clients of the COM+ application. Components developed in .NET are called managed components.

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

Installing .NET assembly in Component Services to run as a COM+ component

Step 1: Open the Visual Studio 2005 Command Prompt and go to the bin directory of the project.

C: \COMPlusObject\COMPlusObject\bin>

Step 2: Execute the following command at the command prompt.

C: \COMPlusObject\COMPlusObject\bin> regsvcs release\COMPlusObject.dll

The console window will show the message given below.

Figure 4: Registering assembly as the COM+ component using VS command prompt

Ignore the warning message since we did not bother about COM security in this example. You can see the files inside the release folder now. It must have added a new file named COMPlusObject.tlb.

Figure 5: Files in release folder after registering assembly as COM+ component

If you want, you can know about these file types (.pdb and .tlb) in the msdn help.

At this moment the .NET managed assembly COMPlusObject.dll is installed and will be running a COM+ component in the system. Let us verity whether it is really installed in the COM list.

To verify the installation of component service:

Step 1: Open the Component Services window Control Panel >> Administrative Tools >> Component Services.

Step 2: In the tree view panel, expand the nodes in following order: Component Services >> Computers >> My Computer >> COM+ Applications.

Figure 6: Verifying installed COM+ application in Component services window

All child nodes under COM+ applications are installed COM+ components. In this list you can see a node named "MyCOMService" which is the application we are looking for. Note that the application named "MyCOMService" is the same one we set in the assembly attribute earlier.

Step 3: Expand MyCOMServices >> Components, now you will see:

Figure 7: Showing .NET object installed as COM+ component in the system

 

Then we are sure here that our assembly is installed as a COM+ service. Isn’t it? OK, this is installed but what next? Is it now accessible to other application like VB6, Access, ASP, ASP.Net web applications, etc? Yes, it should. Let us go for testing it. Next, I am going to show you the procedures to consume the COM+ component in pure ASP page and also in Access application.

Consuming/Using COM+ component in pure ASP page

Step 1: Go to wwwroot of your primary drive: C:\Inetpub\wwwroot (assuming your OS is installed in C drive).

Create a folder named COMAppASPClient inside wwwroot. Create a file CallCOMObject.asp inside COMAppASPClient folder.

Figure 8: Testing asp page setup in the wwwroot

Step 2: Open CallCOMObject.asp file in notepad. Write code to create the object of COMPlusObject.COMService class and calling its function BusinessProcess as shown below. (Note that the BusinessProcess function is the same one we had implemented in the assembly before.)

Figure 9: Code for the ASP page

Save the file.

Step 3: We need to setup the COMAppASPClient folder as a virtual directory in IIS. Open the IIS window as: Control Panel >> Administrative Tools >> Internet Information Services.

Expand the node to reach to the folder COMAppASPClient: Local computer >> Web Sites >> Default Web Site >> COMAppASPClient.

Right click on COMAppASPClient and open the properties window.

Figure 10: Choosing properties option for the directory of ASP page

Click the "Create" button in the application name tag. This makes the directory a virtual directory.

Figure 11: Creating the folder to be a virtual directory

 

(Note: You can skip step 3 if you do not need the ASP page to be accessible from LAN/WAN.)

Step 4: Open an internet explorer window and write the following in the address bar: http://localhost/COMAppASPClient/CallCOMObject.asp

It should show the following result in the page.

Figure 12

It worked. The message string "COM Component Processing Completed" is returned by the COM Component.

Downloads
Conclusion

This example showed how we can use COM+ object (managed) in an unmanaged application of pure ASP. Similarly, we can use it in other applications like VB6, Access, and Visual FoxPro, etc.

No doubt we can also consume these .NET serviced component in .NET windows application, web applications, etc. In these applications you will need to add reference to the COM component to use its classes and functions.

Happy Programming!!!

Abhishek Kumar Singh


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 9:56:49 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search