Using COM components in ASP.NET

Written by: Christoph Wille
Translated by: Bernhard Spuida
First published: 8/28/2000

In today's article I will deal with a topic of interest for most new convertites to ASP.NET: how can I use my own or third party COM components in the ASP.NET environment?

The basic problem lies in the fact that ASP.NET is based on the Common Language Runtime, a so called Managed Execution Environment. The code itself then is called Managed Code and the Runtime obtains the information for management of the code out of the Metadata which was generated by the compiler for the description of the source code.

The problem now is that existing components do not have Metadata information. Therefore, I have to generate the Metadata for them - but stop: if there is a tool capable of generating Metadata, where does that tool take the information for the Metadata from? This comes out of the Type Library and nowadays all components have a Type Library. If not, there is a (crufty) workaround for this, but I'd rather weld a handle onto the component - chuck it!

In this article, I will demonstrate the generation of Metadata using the SA-STMPmailFREE component from Software Artisans as an example. Who wants to follow through the examples can download it without problems - it is free. The download link can be found at the end of the article.

The use of the source code in this article requires an installation of Microsoft's .NET Framework SDK on a Web server. I also assume a certain familiarity of the reader with the C# programming language.

Creating the Metadata DLL

The first step is to create the Metadata. For this we first have to change into the directory of the component DLL in question:

In this directory we start a command prompt and enter the following command:

tlbimp sasmtp.dll /out:sasmtp_dotnetproxy.dll

With this simple command, the Metadata information is saved to the file sasmtp_dotnetproxy.dll (strictly speaking, this DLL is an Assembly). This now is our Proxy for the actual 'unmanaged' COM object. Here is the output for the generation of the proxy for SA-SMTPmail:

Installation for ASP.NET

For ASP.NET there are a few ways of installing the Metadata DLL:

The method of saving the file next to the ASP.NET file has a catch: you have to insert an @Assembly statement into every ASP.NET page that shall use the component. Saving to the bin directory you avoid that - the assembly will be loaded for all ASP.NET pages of the application.

Therefore I of course put the assembly in the bin directory:

The IL Disassembler

Now we come to something that will at first terrify many VB or VBScript programmers - we will have to use a disassembler to get at some important information:

The IL (Intermediate Language) Disassembler can started with the ildasm.exe command entered in the Run dialogbox (accessible via the start menu). The Open command (file menu) selects the respective DLL and in our case we get the following picture:

I have highlighted the Namespace: sasmtp_dotnetproxy. We need this for the @Import statement in our ASP.NET pages. Furtheron there are some nice icons hidden here - a hint: for ASP.NET programmers only the completely blue symbols are important - these are the classes. The one of interest to us is CoSMTPMailClass. This is equivalent to the ProgId SoftArtisans.SMTPMail. As already said, there can be differences in naming!

If we now expand this class, we obtain a list of properties and methods:

As we see, all of the methods have perfectly unequivocal data type assignments: first comes the name of the method, then a colon followed by the return value and then in brackets the parameters of the method.

For VBScript programmers things become unfamiliar when we come to the properties which are represented by get_ and set_ methods:

However there is a listing according to property names (red rhomb). When double clicking a property, we get shown the definition of that property. Attention: properties may be Get, Set or Get/Set.

Take your time toying around with the IL Disassembler - it will certainly come in handy from time to time!

Use of the component in ASP.NET

As we now are well informed about the Metadata, all that remains for us is to actually use the component in ASP.NET. The following example (sasmtpmail.aspx) proves that once all prerequisites have been met, programming is not really difficult any longer:

<% @Page Language="C#" %>
<% @Import Namespace="sasmtp_dotnetproxy" %>
<%
CoSMTPMailClass email = new CoSMTPMailClass();

String strVersion = email.Version;
Response.Write(strVersion);

email.BodyText = "Das ist die Message, \r\ndie abgeschickt wird";
email.ConfirmRead = false;
email.ReturnReceipt = false;
email.FromName = "Christoph Wille";
email.FromAddress = "christophw@alphasierrapapa.com";
email.SMTPLog = Server.MapPath("log.txt");

bool bDone = email.AddRecipient("Chris","christophw@fx2.Dev.AlfaSierraPapa.Com");

email.RemoteHost = "fx2.dev.alfasierrapapa.com";
bool bResult = email.SendMail();

if (bResult)
{
  Response.Write("<br>Mail sent!");
}
else
{
  Response.Write("<br>Error sending mail: ");
  Response.Write(email.Response); 
}
%>

First the Namespace SASMTPlib is imported, followed by the creation of an instance of the class CoSMTPMailClass. In ASP we would have created the component with this statement:

Set xObj = CreateObject("Softartisans.SMTPMail")

The remaining source code isn't different (except for C# vs VBScript) from that of a normal ASP page in which SA-SMTPmail is being used.

Conclusion

One advantage of the method of creating Metadata I have kept back till the end: we work with an object which is in contrast to ASP early bound, thus can be addressed much faster. With this, the extra work has really paid off.

Download of the Code

Click here to start the download.

Links to related Sites

SA-SMTPmail

©2000-2004 AspHeute.com
All rights reserved. The content of these pages is copyrighted.
All content and images on this site are copyright. Reuse (even parts) needs our written consent.