Calling a C# component from Classic ASP

  • Thread starter Thread starter Ben O via DotNetMonster.com
  • Start date Start date
B

Ben O via DotNetMonster.com

Hi Gang,

I've created a C# .NET component which transforms a .TIF file into a .JPEG
file. I have a classic ASP website in which I wish to call this C#
component from my classic ASP.

Can anyone point me in the right direction as to how this may be done. I'm
hungry for some guidance as I can't go forward until this is sorted.

Thanks heaps everyone.


Ben O
 
You can expose your class via COM for use in ASP:

On your class declare a System.Runtime.InteropServices.GuidAttribute("[guid goes here]"). You can obtain a Guid in the appropriate
format by using VS.NET. Go to Tools --> Create GUID. This assigns your soon-to-be COM object that can be used in ASP a unique GUID
that will be registered with Windows.

Your class will be registered with an automattically generated ProgID made up of the Namespace and Class name and with the GUID you
have assigned using the GuidAttribute. Use the ProgIdAttribute if you wish to hard-code a programmatic identifier for your class.

It is recommended that you hard-code the GUID since VS.NET will generate one for you otherwise and it will change upon successive
builds. If neither a ProgID or Guid is specified for your class, it will not be registered for COM Interop unless you specify the
ComVisible attribute as System.Runtime.InteropServices.ComVisible(true).

Again, I recommend just using the GuidAttribute stand-alone.

In the Project settings dialog of the project that contains your class, select the "Configuration Properites" node, then "Build"
node and set "Register for COM Interop" = "true".

When you build your application, VS.NET will register your assembly for COM interop by checking for classes with the attributes I've
mentioned above and if appropriate, registering them with Windows.

This is the command-line utility that you can use to do this manually:

%windir%\Microsoft.NET\Framework\v1.1.4322\RegAsm.exe

In ASP create an instance of the object as you would using a ProgID, for example, with an ADODB.Connection. Here is how you would
access your object via scripting if your class was in the namespace, "MyNamespace" and was named, "MyClass":

VBScript:
Dim conn: Set conn = CreateObject("MyNamespace.MyClass");
JScript:
var conn = new ActiveXObject("MyNamespace.MyClass");

Note that for production servers your going to have to register the assembly manually since it's not a good idea to install VS.NET
and build on live servers. Check out the RegAsm.exe utility that I've mentioned above. It's a command-line utility only.

I suggest creating a Setup and Deployment project for your web site if you are going to be using a live server, and automate the
registration using the installer.
 
In addition to Dave's helpful comments remember that the server must be
running IIS and the .NET framework in order to host your .NET DLL.

You cannot for example put your DLL onto an ASP server running a non windows
OS or without IIS and the framework.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Create a COM callable wrapper to your C# Component and register it via VS.Net
or regasm
 
Back
Top