Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / C#
Article

Call a .NET component from an ASP Page

Rate me:
Please Sign up or sign in to vote.
4.88/5 (13 votes)
7 Dec 2005CPOL2 min read 138.1K   1.3K   30   18
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:

    C#
    using System.EnterpriseServices;
    using System.Runtime.InteropServices;

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

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

    VB.NET
    <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:

plain
>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:

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

VB.NET
<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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
It is an inherent passion for Analytical applications that inspired me to study Physics at the graduate level. During these 3 years I was trained in systematic analysis of physical data and application of Mathematics and in due course of time this passion got transformed into an abiding interest towards automation. This motivated me to go for a higher level application oriented program in Software Development. During the last 6+ years I have enriched myself considerably well by working on various projects which really fine tuned my technical skills. I look forward to put into productive use the knowledge gained / insights developed and at the same time continue to learn enhancing my caliber and pursue a career of excellence, rooted in professional ethics, in the field of Software Development.

I have strong background in C#, ASP.NET, ASP, VB6.0 and SQL server 2000.

Comments and Discussions

 
QuestionGetting 800a01ad error after Dll registration step Pin
Member 1106805126-Nov-17 23:05
Member 1106805126-Nov-17 23:05 
Questionregarding .net componet call from asp Pin
Member 26286779-Sep-15 23:08
Member 26286779-Sep-15 23:08 
AnswerRegister the .Net Component from the Component Service Pin
narayananbabun29-Oct-13 2:56
narayananbabun29-Oct-13 2:56 
In order to call the .Net Component register from the component manually in the path where you are going to access the .Net Component using Regasm "Full Path " / tlb

If this is not working then

Goto Start -> Control Panel -> Administrative tools -> Component Service

Expand and Go to My Computer -> Expand Com + Application
-> Right Click -> New -> Application -> AppName

The New Application will be created

Expand the New App Name
Right Click -> New -> Component -> Browse the Dll folder
Click Ok
GeneralGood Article Pin
srinivas GN27-Oct-10 0:00
srinivas GN27-Oct-10 0:00 
QuestionOs Hardening Automation Pin
Suthari12-Feb-10 7:42
Suthari12-Feb-10 7:42 
GeneralObject Reference not set to an instance of an object Pin
narayananbabun23-Nov-08 20:28
narayananbabun23-Nov-08 20:28 
GeneralYOU ROCK!!! Pin
wdwcrazyva27-Jun-08 12:50
wdwcrazyva27-Jun-08 12:50 
QuestionIntellisense Pin
Brian Silvers10-Sep-07 5:17
Brian Silvers10-Sep-07 5:17 
GeneralError while attempting to create an instance Pin
Amidu)24-Dec-06 7:33
Amidu)24-Dec-06 7:33 
GeneralThis is just a copy of an msdn library page... Pin
jo0ls3-Oct-06 16:02
jo0ls3-Oct-06 16:02 
QuestionCall a .NET component from an VB6.0 [modified] Pin
brahmareddy8131-May-06 0:47
brahmareddy8131-May-06 0:47 
AnswerRe: Call a .NET component from an VB6.0 Pin
Alexander Turlov16-Jan-07 6:32
Alexander Turlov16-Jan-07 6:32 
GeneralI am getting Error Pin
shyamaji_200013-Mar-06 1:09
shyamaji_200013-Mar-06 1:09 
GeneralCOM object not valid or not registered Pin
lieperik20-Feb-06 5:26
lieperik20-Feb-06 5:26 
AnswerRe: COM object not valid or not registered Pin
Alexander Turlov16-Jan-07 6:30
Alexander Turlov16-Jan-07 6:30 
GeneralRe: COM object not valid or not registered Pin
ZarrinPour5-Jun-07 4:09
ZarrinPour5-Jun-07 4:09 
QuestionHelp! Pin
jsanjosembet.es6-Dec-05 21:12
jsanjosembet.es6-Dec-05 21:12 
AnswerRe: Help! Pin
jsanjosembet.es7-Dec-05 12:00
jsanjosembet.es7-Dec-05 12:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.