Saturday, April 05, 2008

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.

5 comments:

Magento Themes said...

i work hard to make it happened but not accessible at the end. thank you now I solve the problem with the help of your blog.

Magento Themes

Web Design Firm said...

When we face any problem in software development where the big learning point start.

- Tanya
Web Design Firm

Magento Templates said...

Surely a great post, really enjoyed as to what all you shared with on your blog.... Well done keep up the good work....

Magento Themes

Anonymous said...

Great posting, cant beleive as to with microsoft dot net this all can be achieved....

sheds to live in

Anonymous said...

A great post with on the topic of Dot Net... Well done keep it up....Commercial Buildings for Sale