Sharing .net library DLLs between Asp classic and Asp.net
Let me first start by saying that I am writing this example after I spent many hours frustrated at the lack of documentation, the abundance of disinformation, and the endless chasing of MSDN dead links. The information contained here is based off many sources all over the net. Though I did not find a single one that clearly laid out how to do it all at once. I will attempt to do that here, in a clear manner; these are the steps that I took to get consistent working results. There may be steps here that are not needed, though I have worked to remove as many of them as time would allow some may have slipped by.
My examples and screen shots will be from
Visual Studio 2005, Standard Edition, Frame work ver. 2.0.5xxx sp2
Web server this example was deployed on was
Windows 2003 Standard running IIS 6.0 / ASP 3.0
The first thing you are going to need to get this project off the ground is a Library DLL. There are a few changes you need to be aware before writing your Library DLL.
Methods should not be static
I am guessing here, due to the fact that asp classic does not support static classes, it stands to reason, it can’t reference one in a DLL library. I attempted a few different was to get this to work and constantly received the following error
“Microsoft VBScript runtime error ‘800a01b6’ Object doesn’t support this property or method: ‘myMethod’.”
My advice, save the headache and avoid the static methods, if you are reading this, and know a solution please comment.
Dll must be marked as Com-visible
To make a DLL com-visible, you need to modify the assembly information. There are multiple ways of doing this, the easiest method is to use visual studio, go to your projects properties then click the “Assembly Information” button and check the checkbox at the bottom of the window that says “Make assembly COM-Visible” (as seen below)
you can also do this by hand by manually editing the AssemblyInfo.cs and adding the following code
Lastly, you can make these changes at run time. I don’t suggest doing that, but if you must, googling “Com-visible runtime” will likely bring you to the right place for it, (I’d post a link to the MSDN page, but in my experience it will change in the future and ultimately be useless.) Another important note, you should not confuse this with, “Register for COM interop” in the build section of the projects properties. In fact you need to make sure this option is UNCHECKED as it will cause problems later, I’ll explain more on this in the following steps.
Your Project Should be Signed
Long story short this step, is done mainly to avoid warnings and possible issues of entering your assembly into the GAC (Global Assembly Cache). This step is easily done using the Visual studio IDE, and Googling “signing assemblies” will show many tutorials on how to do this from the command line if you wish to do so. Signing an Assembly will also protect your libaray from being confused or replaced by other assemblies by mistake, or through nefarious means.
Building the Installer
Now that you have got the prep work done and your code written, you will need to get your library on to your web server, once again this can be done by hand through various command line methods. But the easiest and most convent way for the developer, and web server management is to do this via a setup project, using the installer will add an easy way to add and remove your library to the server. To do this you need to create a new setup project.
Once you have your setup project added to your library solution,
- Click on the setup project in the solution explorer window, you should now see a new set of icons for this type of project
- The second icon is for the “File System Editor” click this and it will bring up a new window for “File System on Target Machine“
- Right click in the left hand side of the window and select “Add Special Folder” select “Global Assembly Cache Folder.”
- Single Click the “Global Assembly Cache Folder.” once highlighted, right click on it and select “Add, the select “Product output“
- In the popup window select “Primary Output“
See screen shot below
At this point, you should be ready to build the project and produce your installer, the setup MSI and setup.exe should be in the setup projects Release or Debug folder depending on the option you chose during build time.
If you are have a .tlb file being placed in your CAG folder along with your primary output, this is a result of having “Register for COM interop” checked in your coding projects build options, to resolve this, either uncheck the box in the build options, or add a “ExcludeFilter” for *.tlb in your installer project.
Copy the Msi and setup.exe over to your web server and install your Library, this should be done from an administrator account. Assuming all went well in your install process you should check the GAC,Typically (c:\windows\assembly) to make sure your library is registered. while here you will need to gather a bit of information so that we can reference the library in our asp.net website. you are going to need the
Public Key Token:
You can do this by right clicking on your assembly and going to properties.
Using the Code
Once you have this information, you are ready to start using your library on your site, first we will cover using the code in asp.net(c#). The first thing we need to do, is to add the assembly to your web.config for your site. To do this, add the following code to your web.config
(you shouldn’t need to add the system.web its there to show placement)
<add assembly="MyAssembly, Version=220.127.116.11, Culture=neutral,PublicKeyToken=8c7fff0114cec090" />
Once you have the assembly information added to your web.config, you can use the assembly like any other referenced library. Here is an Example
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
MyAssembly.Crypto tmpExample = new MyAssembly.Crypto();
string phrase = "test";
string crypted = tmpExample.Encrypting(phrase);
string decrypted = tmpExample.Decrypting(crypted);
Response.Write("Test phrase " + phrase+"<br>");
Response.Write("encrypted " + crypted + "<br>");
Response.Write("decypted " + decrypted + "<br>");
Since you have made the Library available system wide, and made it com-visible, you won’t have to do
anything to make it visible to asp classic, other than adding it to the code itself. Here is an example of how to use the Library in asp classic (vbscript)
Set tmpExample = Server.CreateObject("MyAssembly.Crypto")
phrase = "test"
crypted = tmpExample.Encrypting(phrase)
decrypted = tmpExample.Decrypting(crypted)
Response.Write("Test phrase " + phrase+"<br>")
Response.Write("encrypted " + crypted + "<br>")
Response.Write("decypted " + decrypted + "<br>")
I have tried to be as detailed as possible without being to verbose, but it is possible some details may have slipped past me. So if I have made a mistake, feel free to leave a comment, and I will work hard to resolve the issue. Also, I have found other partial examples on the net that suggest doing the same thing, but in different ways. Some of these examples turned out to be either unnecessary, or completely wrong. What I have posted here is the a method I have found to be the fastest, easiest, and most reliable way of sharing the library DLL between asp.net and asp classic via COM.