Calling .NET Component from a classic ASP page
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 objComFinally, if this page throw any kind of error as Unindentified Class name, then the only solution is to reset the IIS.
set objCom = Server.CreateObject("libraryNameSpace.libraryClass")
objCom.DoSomeWork() ' this method doesn't return any value
Comments