Tips and Tricks from my Experience

Calling .NET Component from a classic ASP page

Posted in Code, COM, JavaScript, Tips, vb.net by Chakravarthy on December 6, 2007

After a long time, today, got a requirement from ASP page. Our current client has a major application written in classic ASP 2.0, and we have given him an extension functionality written in .NET with VB as programming language.

Now, we have 2 different applications running at the same box with 2 different technologies, ie., an ASP application and an ASP.NET application. For both of the applications, there is a common functionality talking to the database via a webservice written in Java. The current situation demand a common library that will be reused at both ASP.NET pages as well as ASP pages. Apart of the requirement, the one point that led me to post here is “How to call a .NET Assembly in a Classic page”

Creating .NET Assembly

Imports System
Imports System.Runtime.InteropServices
Public Class MyLibClass
    Public Function MyLibFunction() As String
        Return " Invoked the method from COM successfully .."
    End Function
End Class

With the above code, you can generate a .NET DLL after compiling with either Visual Basic Command Line Utility, VBC.EXE or from the Visual Studio Environment. While compiling this code, give extra attention at the assembly attribute to make the DLL Visible to COM as mentioned below.

<Assembly: ComVisible(True)> 

If you are using the Visual Studio Environment, go to the Project Properties, at the Compile section don’t forget to check the “Register COM interop” check box. This check box will be by default unchecked, indicating that the DLL will not be exposed to COM.

Now the assembly is ready to reuse. But before we start the actual implementation, if you want the Assembly should be register with the GAC, you have to take the assistance from StrongNameKey file. You can create a Strongname Key file with the command utility SN.EXE or from the Visual Studio as well. The command to create the Strongname Key file is as mentioned below

> sn -k strongnamekeyfile.snk

Once you are done with the strong name key file generation, you can attach the key file either with the /keyfile:strongnamekeyfile.snk option or with the assembly attribute as <Assembly: AssemblyKeyFile(“strongnamekeyfile.snk”)>

Now it is the final time to compile your code and do the calling stuff from ASP page. Once you compile the code from Visual Studio, you will have 3 files, .PDB / .TLB / .DLL. The .DLL is the actual file. You have to do 2 things to share this DLL.

Register as COM as well as with GAC

Now that the DLL is ready, we will take the assistance of REGASM command line utility to register as public assembly.

> regasm /codebase /tlb:a.tlb OurAssemblyFile.dll

With this command, the DLL will be registering at the registry. Please don’t forget to use the /codebase option. To register the same with GAC use the following command

> gacutil /if OutAssemblyFile.dll

This command will register with the GAC and is available as reference within the .NET Framework. With the first command, the object is ready to invoke from ASP code. You can use the COM component with Server.CreateObject(“OurAssemblyFile.ClassName”) command from ASP page. The actual implementation at any ASP page will look like as mentioned below

 dim objCom 
 set objCom = Server.CreateObject("libraryNameSpace.libraryClass")
 objCom.DoSomeWork() ' this method doesn't return any value

Finally, if this page throw any kind of error as Unindentified Class name, then the only solution is to reset the IIS.

Advertisements

12 Responses

Subscribe to comments with RSS.

  1. Ravi Shankar said, on December 14, 2010 at 5:53 am

    superb.Thanks chakravarthy!

  2. Ravi Shankar said, on December 14, 2010 at 9:09 am

    I have did all that mentioned above.But still getting error below while running my asp page.

    Server object error ‘ASP 0177 : 800401f3’
    Server.CreateObject Failed
    Invalid class string

    Any help is greatly appreciated.

    • Mukesh said, on June 14, 2011 at 7:39 pm

      Is your problem resolved?

  3. lim said, on March 15, 2011 at 9:44 am

    do your class have intelligence sence for DoSomeWork methods after objCom. {dot}

    I am using VS2010 buiding an vb.net DLL and coding in ASP classic page, but non intelligence sense work. It happend only for my writing DLL.

  4. Haseeb said, on March 24, 2011 at 3:40 pm

    Hi,

    Iam also getting the same error :
    Server object error ‘ASP 0177 : 800401f3’

    Server.CreateObject Failed

    I followed all the steps mentioned above. Please help.

    Thanks,
    Haseeb

  5. Lalita Mittal said, on April 11, 2011 at 8:20 am

    Hi,

    Iam also getting the same error :
    Server object error ‘ASP 0177 : 800401f3′

    Server.CreateObject Failed

    Please help!!!

    • Mukesh said, on June 14, 2011 at 7:38 pm

      Is this problem resolved?

  6. […] Calling .NET Component from a classic ASP page | Tips … – Dec 06, 2007 · do your class have intelligence sence for DoSomeWork methods after objCom. {dot} I am using VS2010 buiding an vb.net DLL and coding in ASP classic …… […]

  7. Bheeshm Prakash Nayak said, on May 18, 2017 at 2:19 pm

    Great Stuff.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: