Vishwamohan

Welcome to Vishwa's blog - Technology, Spirituality and More...

Make a .NET Component accessible in Classic ASP or VB6

I came across a situation again to use exactly same .NET component as COM component in classic ASP page. I had totally forgotten these steps but finally managed to make it work. Later, I thought to put these steps together on my blog, so that if anyone else also don’t know or forget these steps, this will help.


 

1.       In AssemblyInfo.vb -- Add Following NameSpaces, you will have to add their corresponding references
Imports System.Runtime.InteropServices
Imports System.EnterpriseServices
 
2.       Add Following Assembly Attributes
<Assembly: ApplicationName("AppName")>
<Assembly: ApplicationActivation(ActivationOption.Server)>
<Assembly: ApplicationAccessControl(False, AccessChecksLevel:=AccessChecksLevelOption.ApplicationComponent)>
<Assembly: ComVisible(True)>
 
3.       Make sure AssembyVersionAttribute is set to a static value (i.e. 1.0.0.0 not 1.0.0.*)
<Assembly: AssemblyVersion("1.0.0.0")>
 
4.       Make Sure you class has a Public Default Constructor, thus no singleton design pattern or shared Methods.
 
5.       In Signing tab Check Sign the assembly and give a name for strong name key.
 
6.       Compile your project and create the dll.
 
7.       Go to C:\Windows\Microsoft.NET\Framework\v2.0.50727 folder and then run following command to create and register type library,  on command prompt type appropriate path and dll name
 
>RegAsm <PathName>\ProjectName.dll /tlb /codebase
 
Note: You will get a message that Types Registered Successfully. It will create a .tlb (type library file) in the same folder where your dll exist.
8.       Now you can refer the type library file into your Classic ASP Page.
 
9.     If you change and recompile first unregister by adding a switch /unregister of Command Line Param in #7, If you deploy this component on a different machine, perform the step #7 again on that machine.
 
10.   You are ready to use the .NET component in ASP Page now.
 
If Component reads Web.Config file, then you have to do some extra steps
1. In "bin" folder - Create 2 files
              a. Create "application.manifest" file with following content
                          <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
                         <assemblyxmlns="urn:schemas-microsoft-com:asm.v1"manifestVersion="1.0"/>

 

 

                       b. Create "application.config" with needed section(s) from Web.Config

       2.  Go to COM+ package through Component Services

                     a. Add your type library file (.tlb ) in your package

                     b. In Properties of the Package go to "Activation" tab  and  change Application Root Directory to <APP_PATH>\bin


     Working Example:  For example let’s create a Class Library Project – Vishwa.Example.ComTest

File: AssemblyInfo.vb
 Class Name : TestProcess.vb
   1:  Imports System
   2:  Imports System.Reflection
   3:  Imports System.Runtime.InteropServices
   4:  Imports System.EnterpriseServices
   5:   
   6:  #Region "Assembly Attributes"
   7:  <Assembly: AssemblyTitle("Vishwa.Example.ComTest")> 
   8:  <Assembly: AssemblyDescription("")> 
   9:  <Assembly: AssemblyCompany("")> 
  10:  <Assembly: AssemblyProduct("Vishwa.Example.ComTest")> 
  11:  <Assembly: AssemblyCopyright("Copyright © 2008")> 
  12:  <Assembly: AssemblyTrademark("")> 
  13:  <Assembly: ApplicationName("ComTest")> 
  14:  <Assembly: ApplicationActivation(ActivationOption.Server)> 
  15:  <Assembly: ApplicationAccessControl(False, AccessChecksLevel:=AccessChecksLevelOption.ApplicationComponent)> 
  16:  <Assembly: ComVisible(True)> 
  17:  <Assembly: Guid("1e411fb7-d23f-4246-99da-4200e999e030")> 
  18:  <Assembly: AssemblyVersion("1.0.0.0")> 
  19:  <Assembly: AssemblyFileVersion("1.0.0.0")> 
  20:  #End Region
   1:  Public Class TestProcess
   2:   
   3:  #Region "Default Public Constructor"
   4:      Public Sub New()
   5:      End Sub
   6:  #End Region
   7:   
   8:  #Region "Public Methods"
   9:   
  10:      Public Function Add(ByVal value1 As Integer, ByVal value2 As Integer) As Integer
  11:          Return value1 + value2
  12:      End Function
  13:   
  14:  #End Region
  15:   
  16:  End Class

In Order to Test in Classic ASP page
Dim obj: Set obj = Server.CreateObject("Vishwa.Example.ComTest.TestProcess")
Dim returnValue
returnValue = obj.Add(2, 3)
Note: I was not able to see the method name Add in intellicense  but it works.

Comments (2) -

  • Kourosh Saleh

    1/30/2009 4:08:45 PM | Reply

    Hi,
    Thanks alot for your helpful blog.
    I have a question. Why it does not work if you change ActivationOption to ActivatioinOption.Library?

    thanks again

  • Larry West

    6/12/2010 2:05:32 AM | Reply

    Dear Mr. Kumar,

    I am faced with the same situation you described in this post and deeply appreciate you sharing what you discovered about this process.  I was hoping that you had some insights on why this process isn't working for me.  I have a simple Hello World class created in VB.Net 2008 and am attempting to call it from a classic ASP page running on Windows 2008 Server with IIS 7.  I followed your instructions 1-9 precisely but still get the Active X error, "Unable to create the component" on the line in which the server.createobject() method appears.  

    Are there any other issues, such as permissions on the DLL file or anything else that may need to be considered to make this scenario work?

    Thank you again for the helpful information and if you have any additional thoughts I would deeply appreciate them.

Loading