using a C# class library in both COM and .NET code?

J

jason

i have a C# class library full of objects.

the immediate need was to instantiate these objects in classic ASP, so
i inherit ServicedComponent and build the project with COM Interop set
to true. and the objects work in classic ASP just fine.

however, we are slowly introducing ASP.NET code, and i would like to
make use of the same objects. however, i don't want to use the old COM
method of Server.CreateObject(). i would like to use Import/using
statements to gain access to everything in the namespace.

but when i try add a reference to the .NET application for the class
library, it says:

A reference to 'MyLibrary' could not be added. Converting the type
library to a .NET assembly failed. Type library RCObjects was exported
from a CLR assembly and can not be re-imported as a CLR assembly.

it seems amusing that it can import a COM library into .NET, so long as
the COM library wasn't originally coded in .NET.

so my question is, what's the most appropriate way to make this library
available to both the classic ASP and ASP.NET code?

thanks for any help,

jason
 
N

Nicholas Paldino [.NET/C# MVP]

Jason,

If you want to use this library in .NET code, then why not just add a
reference to the dll itself, and not to the TLB that you export so that you
can use it in COM?

Hope this helps.
 
J

jason

huh! manually browsing for the .DLL did the trick. how will this be
resolved during runtime though, i wonder?

thanks though. i can at least start coding with this help
 
N

Nicholas Paldino [.NET/C# MVP]

Jason,

What you are doing is two completely different things. When you
registered through COM (which you did incorrect, btw, you need to call
regsvcs to register assemblies for COM+, I believe), it created a TLB and
then registered that. .NET uses a completely different mechanism to find
assemblies.

Chances are your assembly is in the GAC as a result of the COM
registration, so I wouldn't worry. If it isn't, then place a copy of the
..NET assembly in the bin subdirectory of your ASP.NET application, and it
will find it just fine.
 
J

jason

ok, that makes sense. i remember reading a little blurb about the .NET
library in the /bin subdirectory thing. that should be no problem.

what makes you think i misregistered the COM library, though? i never
even mentioned how i did it. i use the COM+ MMC to register the
com-wrapped library, and it works just fine. what's wrong with that
method?
 
N

Nicholas Paldino [.NET/C# MVP]

Jason,

If you have a class that derives from ServicedComponent, you need to
call the regsvcs utility to correctly register it with COM+ so that it can
be used by COM components AND by .NET. Registering the TLB through COM+
isn't sufficient if you want to get the same benefits in your .NET code.
 
J

jason

ahh, i see! i appreciate the information. i read the MSDN articles on
the regsvcs utility, and ran the following:

regsvcs /appname:ExistingMyApplication MyLibrary.dll

and it did register the components successfully in COM+ ... however,
none of the ASP classic pages can successfully execute a
Server.CreateObject now that i've used that utility.

is there something i'm missing to successfully register the COM
portion? the articles say that the TLB is created and registered by
default. or perhaps if it is more efficient, do you know of other good
references for the regsvcs.exe utility?
 
W

Willy Denoyette [MVP]

jason said:
ahh, i see! i appreciate the information. i read the MSDN articles on
the regsvcs utility, and ran the following:

regsvcs /appname:ExistingMyApplication MyLibrary.dll

and it did register the components successfully in COM+ ... however,
none of the ASP classic pages can successfully execute a
Server.CreateObject now that i've used that utility.

is there something i'm missing to successfully register the COM
portion? the articles say that the TLB is created and registered by
default. or perhaps if it is more efficient, do you know of other good
references for the regsvcs.exe utility?

What error is returned by the CreateObject call?
Is your assembly stored in the GAC?
Did you try to call your method from a simple script using VBScript or
JScript?

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top