Click here to Skip to main content
Click here to Skip to main content

Call a .NET component from an ASP Page

By , 7 Dec 2005
 

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

License

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

About the Author

Manoj Kumar G (IND)
Web Developer
India India
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGood Articlemembersrinivas GN27 Oct '10 - 0:00 
Hi Manoj,
 
By reading the above article i am able to call the .net method in ASP 3.0.
Very nice article.
 
Thank you
Srinivas Gulla.
QuestionOs Hardening AutomationmemberSuthari12 Feb '10 - 7:42 
Hi all,
 
Am working presently on my final year project on .Net.
for os hardening automation,Can anybody help me out how to extract the
.bat file to .net in the web form and remotely i should be able to apply on
to the server,
It will be really greatful if any one help me
GeneralObject Reference not set to an instance of an objectmembernarayananbabun23 Nov '08 - 20:28 
I created object as how it was described but now i am getting an error in the line where i use the object to call a function. Please tell me what i am missing
GeneralYOU ROCK!!!memberwdwcrazyva27 Jun '08 - 12:50 
I spent almost all day trying to find a simple sample like this. It just took putting the right combination of words into Google I guess. This solved a huge problem I was was having in no time!!
 
THANK YOU~!!!! Big Grin | :-D
QuestionIntellisensememberBrian Silvers10 Sep '07 - 5:17 
I noticed when I created the object in my VS2003 solution that we have a mixed ASP/.net solution that I wasn't getting intellisense. I did however get the component working since I knew what attributes it had. I modified my class to inherit an interface as well as [ClassInterface(ClassInterfaceType.None)]
 
Is there anything else I can do that will give me intellisense on the object?
 
Thanks,
Brian
GeneralError while attempting to create an instancememberAmidu)24 Dec '06 - 7:33 
Laugh | :laugh: Smile | :) Hi,
 
Great Article.
I followed all the steps that were mentioned, and it seems that everything went fine (I didnt get any error messages). When I tried to create an Object I got an Error message:
 
Server object, ASP 0177 (0x800401F3)
Invalid class string
 
I checked that the name of the Object that im creating is identical to the name of the ApplicationName attribute, and that the class name corresponds to the one in the source.
 
beside that I copied the source as is.
 
I used the .net v2 framework.
 
What might be the problem for you opinion?
 

 

 
Thank you!
Ami
GeneralThis is just a copy of an msdn library page...memberjo0ls3 Oct '06 - 16:02 
"Creating a CRM Post-Callout Object Using Visual Studio .NET"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/crmsdk1_2/htm/v1d2creatingacrmpostcalloutobjectusingvisualstudio.asp
or "post-callouts" -> "Creating a CRM Post-Callout Object Using Visual Studio .NET"
QuestionCall a .NET component from an VB6.0 [modified]memberbrahmareddy8131 May '06 - 0:47 
Help me
I have developed an Assembly in C#.net. Now I want to use that assembly in VB6.0 where the .NET framework is not installed. How can I Proceed.
 
-- modified at 6:48 Wednesday 31st May, 2006
AnswerRe: Call a .NET component from an VB6.0memberAlexander Turlov16 Jan '07 - 6:32 
Start from installing .NET framework. Without it your C#.NET component wil never work. Then you need to register your .NET component as a COM component and there you go calling it from your VB 6 code. Hope this helps.
 
Alexander Turlov
Software development consultant, MCSD.NET

GeneralI am getting Errormembershyamaji_200013 Mar '06 - 1:09 
Hi
I have downloaded the code and start using it thru VB6.0. but it is not giving the desired output
I have followed the instructions as mentioned.
 
Shyamaji

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 8 Dec 2005
Article Copyright 2005 by Manoj Kumar G (IND)
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid