Pages

Friday, December 7, 2007

COM/COM+ in ASP.NET

Microsoft introduced a new concept called managed code. Code that runs within the CLR environment is called managed code. Code that runs outside the boundaries of CLR environment is called unmanaged code. Some of the example of unmanaged code are COM, ActiveX components. .NET provides a mechanism which makes it possible to use the existing unmanaged components with ASP.NET application. This mechanism is called interoperability.

Introduction to COM/COM+
COM is a binary standard. It is acronym for the Component Object Model. COM is a specification and the components built in any language according to this specification (COM) can be integrated with any client application written in any language. The Client doesn't has to know the location of the components. When working with COM, the objects are compiled into DLLs i.e. the binary executable files. A DLL can contain more than one class.
COM+ is nothing but an extension of COM. COM+ is a combination of operating system services and an object oriented programming architecture. By extension to COM,it means adding a few services, like event registry, the queues of asynchronously received requests for a service (transaction management), ensuring security against the system requests (authentication), object pooling.

Invoking COM/COM+ components
In .NET, all codes are executed within the Common Language Runtime (CLR) environment. CLR runs your code and specifies services required for the development process. ASP.NET application cannot directly interact with COM components.

When an ASP.NET application calls a method defined in the COM component, CLR creates a wrapper called Runtime Callable Wrapper (RCW). RCW is a .NET object and is executed in CLR. The primary task of RCW is to marshal calls between the >NET application and the COM components. The CLR only creates one RCW object for a COM component even if it is referred more than one time in an application. RCW stores the number of references attached to it in an internal cache. RCW is created on the heap and so is subjected to garbage collection. When reference count to a particular RCW becomes zero, the garbage collection module will delete it and the memory allocated to the RCW object will be released.

When an ASP.NET application calls a method defined within the COM component, RCW performs the following tasks:

1.)Creates an instance of the COM object and bind it to the application
2.)Manages data conversion between managed and unmanaged data type.
3.)Manages the life time of the COM component for which it is acting as a wrapper.
4.)Handles exception during the data conversion process.

To add a COM component to an ASP.NET application,perform the following steps:

1. Open new project and select Visual C# projects from the Project types pane and ASP.NET Web Service from the templates pane.

2. Select Project->Add References.

3. Click the COM tab in the Add Reference dialog box. A list of COM components is displayed.

4. Select the COM component that you want to use within your ASP.NET application. For example, select the Microsoft ActiveX Data Objects 2.7 library option form the list and double click the Select button. The component is added to the Selected Components pane.

5. Click the OK button. The component that you have added to the project is displayed in the Reference node in Solution Explorer.

6. You can use the COM component in the application by specifying its library class, as follows:
ADODB.RecordsetClass rs=new ADODB.RecordsetClass();

This COM object rs can be used in the application to call its method.

No comments: