65.9K
CodeProject is changing. Read more.
Home

Call a .NET component from an ASP Page

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (11 votes)

Nov 29, 2005

CPOL

2 min read

viewsIcon

139675

downloadIcon

1270

How to call a .NET component from VB6.0/ASP developed in either Microsoft Visual Basic® .NET or Microsoft Visual C#® .NET

Introduction

The following steps outline how to call a .NET component from VB6.0/ASP developed in either Microsoft Visual Basic® .NET or Microsoft Visual C#® .NET.

PART - I

  1. Launch your Visual Studio .NET

  2. Open your .NET Class Project

  3. Add a reference to the System.EnterpriseServices namespace

  4. At the top of the code file, add the following statements to specify the namespaces needed:

    using System.EnterpriseServices;
    using System.Runtime.InteropServices;

    Imports System.EnterpriseServices
    Imports System.Runtime.InteropServices
  5. Add the following statements before the namespace declaration to set the assembly options:

    [assembly: ApplicationName("ClassLibrary1")]
    [assembly: ApplicationActivation(ActivationOption.Server)]
    [assembly: ApplicationAccessControl(false,
    AccessChecksLevel=AccessChecksLevelOption.ApplicationComponent)]

    <Assembly: ApplicationName("ClassLibrary1")>
    <Assembly: ApplicationActivation(ActivationOption.Server)>
    <Assembly:ApplicationAccessControl(False, 
        AccessChecksLevel:=AccessChecksLevelOption.ApplicationComponent)>

ClassLibrary1 – Sample Project

These attributes help specify how the .NET assembly will operate with COM+ services. The ApplicationName attribute allows one to specify the name of the COM+ services application that will host the .NET assembly when it is imported into COM+. You can specify any name you would like here. The ApplicationActivation attribute specifies the type of COM+ application being created. This attribute uses an enumeration called ActivationOption which specifies that the application is a server application. This means that when the component is activated, it will be hosted in a process separate from the process that called it. The ApplicationAccessControl attribute specifies whether the component will perform any access level checks.

PART - II

Generate a strong name for the assembly using the .NET Framework Strong Name Utility (sn.exe). This utility generates a public/private key file that will be incorporated into the assembly. To create this key file, go to a command prompt and change directories to the location where the solution is stored.

Type in the command:

>cd C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin
>sn –k “D:\DotNet Testing\ClassLibrary1\ClassLibrary1\obj\Debug\mykey.snk”

This will create the key file.

Add the strong file name to the assembly by adding an attribute in the AssemblyInfo code file that is part of the project. Locate the AssemblyInfo file in Solution Explorer and click it. In the code file, add the following attribute:

[assembly: AssemblyKeyFile(@"mykey.snk)] 

<Assembly: AssemblyKeyFile("mykey.snk")>

This attribute looks for the strong name key file that was previously created in the folder that was created for the solution. If there is an AssemblyKeyFile attribute in the AssemblyInfo file, replace it with this one.

  1. Build the solution. This will create the DLL file.

  2. Create type library file using your DLL file - Register for COM Interop.

    1. Place the DLL file to your virtual directory /bin folder.

    2. Type in the command:

      >cd “C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322”
      >regasm "D:\DotNet Testing\asp\bin\ClassLibrary1.dll" /tlb /codebase

      This will create the TLB file in your bin folder.

    3. Finally, you can call .NET components from VB6 or ASP:

      Dim Obj
      Set Obj = Server.CreateObject("ClassLibrary1.Class1") 
      Obj.PostCreate