Home > C# > Sharing .net library DLLs between Asp classic and Asp.net

Sharing .net library DLLs between Asp classic and Asp.net

Preface

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)
com-visible
you can also do this by hand by manually editing the AssemblyInfo.cs and adding the following code

[assembly: ComVisible(true)]

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.
signingbuild


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.

newproject

Once you have your setup project added to your library solution,

  1. Click on the setup project in the solution explorer window, you should now see a new set of icons for this type of project
  2. 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
  3. Right click in the left hand side of the window and select “Add Special Folder” select “Global Assembly Cache Folder.”
  4. Single Click the “Global Assembly Cache Folder.” once highlighted, right click on it and select “Add, the select “Product output
  5. In the popup window select “Primary Output

See screen shot below

setup
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.

Note:
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.


Installing

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
Name:
Culture:
Version:
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)

<system.web>
  <compilation>	
    <assemblies>
      <add assembly="MyAssembly, Version=1.0.0.1, Culture=neutral,PublicKeyToken=8c7fff0114cec090" />
    </assemblies>
  </compilation>
</system.web>

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)

Dim tmpExample 
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>")


Conclusion

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.

  1. tony
    November 30th, -0001 at 00:00 | #1

    Hi Bruce,

    I’ve seen many of these types of tutorials, but I like your suggestion
    of creating an install instead of using gacutil and other manual processes
    for making the .Net DLL available to unmanaged code (eg. ASP).

    I’ve followed your steps, and successfully created the install application,
    and created the assembly in the GAC.

    When I add the assembly reference in the web.config file, and try to use
    that reference in a page codebehind, neither the assembly nor the class
    is visible in intellisense. When I go ahead and try to instanciate the
    class anyway, I get an error stating the type or namespace cannot be found.

    I also noticed that I was unable to reference any of the assemblies in
    the web.config in the manner you described in this blog.

    Am I missing something?

    Thanks.

    Tony

  2. October 5th, 2009 at 10:09 | #2

    @tony
    Sorry for the delayed response on this Tony, But you will also need to install the .Dll in the GAC on your local development system as well as the web server you are deploying to. I used the same installer I made to install on the webserver on my local system, and have had no issues, if this doesn’t resolve your issue please let me know.

  3. Deepali
    April 12th, 2010 at 06:32 | #3

    this is very nice article i hav ever seen regarding DLL. I followed your steps even my dll get created in C->Window->Assembly
    bt its methods are not available in my classic asp. Sorry bt plz can you explain installation process in detail and the purpose of it.Thanx

    • April 19th, 2010 at 20:51 | #4

      Firstly, thanks for the kind words, glad to see its helping someone.

      Secondly, can you access the DLL in asp.net, if you can access the DLL from the GAC in .net, and not asp classic, it has more to do with the way your DLL was authored, and is not an installation problem. The Classes can not be static, as asp classic doesn’t support static objects as far as I know. If that is not the issue, let me know, and we can see if we can’t explore the issue further.

  4. June 29th, 2010 at 10:26 | #5

    Hi Bruce,

    Nice article. I wish i stumbled upon this a day ago! After many struggles, I’ve compiled my dll and am able to create an instance of the object in classic ASP. My issue is that none of my methods appear to working. I am getting, “Object doesn’t support this property or method”. None of my methods are static so I’m not sure why this is. If I test the dll in a .net project it works properly. Any ideas? Many thanks in advance.

    • June 29th, 2010 at 10:40 | #6

      I’m not sure, I would have to see the code to get a better idea, if you want to email me the code you can send it to bruce@bruceburge.com, either way best of luck with it.

  5. June 30th, 2013 at 18:06 | #7

    Hi,
    Actually I have been using lot of managed code with ASP in the same manual way registeing into GAC and accessing the object. But those were with dlls creted with vb.net. Yesterday happened to create a dll in c# and access the object, which was throwing error, so I wapped the class in vb.net class and registered the vb.net class library in the usual way, added to GAC and I was able to create the object in ASP. Is there any specific reason for this? Any idea?
    Thanks..in advance.

    • July 1st, 2013 at 03:16 | #8

      What was the error, without knowing that, its hard to pinpoint an issue, but if I were to guess, I would see if the C# DLL contains static methods, because you can consume them in vb.net, I don’t believe you can directly in asp classic.

  1. No trackbacks yet.