COM Objects, Early Binding, and Server-Side ASPX Compilation

G

Guest

Summary: I've got some *.ASPX pages that still use COM objects. I'd like to
enable Option Strict, but I get "error BC30574: Option Strict On disallows
late binding" errors. How can I bypass this problem WITHOUT moving my code
out of the *.ASPX page or pre-compiling it, and WITHOUT rewriting the COM
objects as managed .NET code?


Disclaimer: I understand there is a performance penalty when accessing COM
objects from managed code, and long to convert these objects to managed code
one day. However, that is not an option right now. Also, I understand there
are benefits to pre-compiled code-behind files, however again I'm hoping for
a solution without involving new code-behinds.


Example:

Let's say I'm writing a VB.NET application using Visual Studio, and I want
to instantiate the COM class "FruitBasket.CAppleBasket". I don't need to use
a CreateObject call to do this!

First, I'd add that COM object to my project's "References" folder, as a COM
reference.

Second, presumably, I could write code like:
Dim oBasket as New FruitBasket.CAppleBasket

Okay, great, so that's the VB.NET/Visual Studio world as I understand it,
which seems to thwart complaints of late binding (please correct me if I'm
wrong).


Now, let's look at a snippet of my (pretend) ASPX page!

<%
Dim oBasket as Object = Server.CreateObject("FruitBasket.CAppleBasket")
oBasket.setColor("red")
%>

If my @Page directive sets Strict="True", then I see compilation errors like
"error BC30574: Option Strict On disallows late binding".

So I'm left wondering, what (if anything) can I do to prevent late binding
to these COM objects without significant code changes? Is there a Web.config
entry, or @Import directive I can use for this?

Thanks!
 
P

Patrick Steele [MVP]

Now, let's look at a snippet of my (pretend) ASPX page!

<%
Dim oBasket as Object = Server.CreateObject("FruitBasket.CAppleBasket")
oBasket.setColor("red")
%>

If my @Page directive sets Strict="True", then I see compilation errors like
"error BC30574: Option Strict On disallows late binding".

You've defined oBasket as type "Object". If you define it as type
"CAppleBasket", you shouldn't have the error.
 
G

Guest

Thanks for taking a stab at it!

I understand how declaring my variable as the actual type would avoid
late-binding errors, but instead I see a different error:

error BC30002: Type 'CAppleBasket' is not defined.

(And similar errors for attempts at "New FruitBasket.CAppleBasket", "New
Interop.FruitBasket.CAppleBasket", etc).

Keeping in mind that CAppleBasket is a COM object, what can I do to make its
type known to my ASP.NET application?

Thanks again!
 
B

bruce barker

..net can not directly access a com object. you either use the pinvoke
routines, or create a wrapper around the com object. VS will create the
wrapper if you add a reference of the com object to the project, else use
the commandline utility tlbimp.exe.

-- bruce (sqlwork.com)


| Thanks for taking a stab at it!
|
| I understand how declaring my variable as the actual type would avoid
| late-binding errors, but instead I see a different error:
|
| error BC30002: Type 'CAppleBasket' is not defined.
|
| (And similar errors for attempts at "New FruitBasket.CAppleBasket", "New
| Interop.FruitBasket.CAppleBasket", etc).
|
| Keeping in mind that CAppleBasket is a COM object, what can I do to make
its
| type known to my ASP.NET application?
|
| Thanks again!
|
| "Patrick Steele [MVP]" wrote:
|
| > In article <[email protected]>,
| > (e-mail address removed) says...
| > > Now, let's look at a snippet of my (pretend) ASPX page!
| > >
| > > <%
| > > Dim oBasket as Object =
Server.CreateObject("FruitBasket.CAppleBasket")
| > > oBasket.setColor("red")
| > > %>
| > >
| > > If my @Page directive sets Strict="True", then I see compilation
errors like
| > > "error BC30574: Option Strict On disallows late binding".
| >
| > You've defined oBasket as type "Object". If you define it as type
| > "CAppleBasket", you shouldn't have the error.
| >
| > --
| > Patrick Steele
| > Microsoft .NET MVP
| > http://weblogs.asp.net/psteele
| >
 
P

Patrick Steele [MVP]

I understand how declaring my variable as the actual type would avoid
late-binding errors, but instead I see a different error:

error BC30002: Type 'CAppleBasket' is not defined.

(And similar errors for attempts at "New FruitBasket.CAppleBasket", "New
Interop.FruitBasket.CAppleBasket", etc).

Keeping in mind that CAppleBasket is a COM object, what can I do to make its
type known to my ASP.NET application?

If your COM object was developed in VB6, the "CAppleBasket" actually
represents the default interface to the COM object. The actual
createable class would be CAppleBasketClass. Try:

dim basket as FruitBasket.CAppletBasket = New
FruitBasket.CAppletBasketClass()
 
G

Guest

Thanks! Sorry for the delay, but this worked, and was exactly what I was
looking for.

The only other thing I needed to do was give the impersonated user (e.g.
aspnetuser) read/execute access to that DLL on the filesystem.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top