In the course of upgrading from IIS 6 to IIS 7 and Windows Server 2008 I was reluctant to bring with me all the legacy DLLs our code uses. For instance, we had several references to the CDONTS object in our ASP code. Unfortunately, CDONTS is not included on the new version of Windows Server. While several options to replace it are readily available, none of them would allow me the pleasure of writing this tutorial. (If you're interested in an explanation of the EmailServe object that I'm registering with COM see the appendix of this article here.) This tutorial is a guide to registering a .NET DLL with COM so that you can access it from ordinary ASP code.
This part of the tutorial will probably seem a bit... easy, but this tutorial wouldnt be complete without it.
- Open up Visual Studio. I'm using VS2008, but the concepts should be similar for newer versions of Visual Studio.
- Create a new Class Library. (My appendix covers the code in VB, but if you wish to use another .NET language, all the concepts should be the same.)
- Select Class1.vb (or whatever the default class added to the project is called for you) and delete it.
- Go to "My Project."
- Optional: Go to the "Compile" tab and check "Register for COM interop." This will add it to the build computer''s COM interop. If you wish to test to your project on the build computer you'll need to do this, then you can skip the deployment section. Otherwise, skip this step. (Note: I guess the Express Editions of Visual Studio don't have this option. You''ll have to either upgrade or take the deployment route I discuss later.)
- Now click "Signing." Check the "Sign the assembly" box. Then choose "" from the dropdown and fill in the information in the popup. The name does not really matter. This ensures that the assembly can be registered and not interfere with any other assembly on your computer.
Now right click on our project, select "Add New Item" and select COM Class. The only difference between a COM class and a plain old class the line:
<ComClass(MyClass.ClassId, MyClass.InterfaceId, MyClass.EventsId> _Which needs to be placed directly above your class declaration. Our class needs to have those fields declared inside like so:
#Region "COM GUIDs" Public Const ClassId As String = "b3094450-a3d7-4c0d-97d2-432fb8f94e0f" Public Const InterfaceId As String = "705f1451-ce8b-4baf-a3ef-7ea20b8ef7d9" Public Const EventsId As String = "6f34819b-0fe2-4fde-9a5f-bd6683b6ada3" #End RegionGUIDs can be generated inside Visual Studio.
- Code your class!
Deploying your DLL to your web server takes two different command line tools: the Global Assembly Cache Tool (Gacutil.exe) and the Assembly Registration Tool (Regasm.exe). These tools come packaged with the .NET SDK. The easiest way to access them is through the .NET SDK Command Prompt since you'll be able to call the commands without needing to switch directories.
On my server the commands look like this:
regasm D:\Assemblies\bin\asp_myclass.dll /tbl:asp_myclass.dll gacutil -i D:\Assemblies\bin\asp_myclass.dll
When you are finished RESTART IIS.
Unregistering the DLL
I know what you're thinking, "Didn''t we just finish registering this DLL?" Well, lets say that shortly after finishing the DLL's registration you discover a glaring error in your programming (ok, in someone else's programming.) Simply re-registering the DLL will not correct the problem. You have to start over.
On our servers these commands read:
regasm -u D:\Assemblies\bin\asp_myclass.dll gacutil -u asp_myclassRestart the website's IIS.
Now you can start the install process over again.
Well, that does it for accessing a .NET library through COM. Youll now be able to access your .NET