COM object in static field

G

Guest

Hi all

I has Component inside ASP.NET page that create CO
object and store reference to it in the Component's privat
static field. Then I cast this reference to desired interfac
and call interface method. All works fine.(No exception throwed

Then I push button and the same ASP.NET page initiated again
Components private static field has reference to previously create
COM object. But when I try to cast this reference to COM interfac
I get InvalidCastException 'Specified cast is not valid.
I can not understand why. May be RCW lose pointer to interfac
between requesting page. Intresting if I create COM objec
on each page loading instead of storing reference to it in th
static field all works fine. And last, all of this with static field
with reference to COM in Windows Forms app works very well

ASP.NET and COM use STA threading model
I use custom RCW to interoperate with COM
I use ASP.NET 1.

Thanks.
 
H

Hans Kesting

Alexander said:
Hi all.

I has Component inside ASP.NET page that create COM
object and store reference to it in the Component's private
static field. Then I cast this reference to desired interface
and call interface method. All works fine.(No exception throwed)

Then I push button and the same ASP.NET page initiated again.
Components private static field has reference to previously created
COM object. But when I try to cast this reference to COM interface
I get InvalidCastException 'Specified cast is not valid.'
I can not understand why. May be RCW lose pointer to interface
between requesting page. Intresting if I create COM object
on each page loading instead of storing reference to it in the
static field all works fine. And last, all of this with static fields
with reference to COM in Windows Forms app works very well.

ASP.NET and COM use STA threading model.
I use custom RCW to interoperate with COM.
I use ASP.NET 1.1

Thanks.

In a Windows Forms app, the pages are objects that (can) remain in memory.
In the ASP.Net environment, these objects remain alive just long enough to
process the page. When the HTML is sent to the client, the page-object is
destroyed. If you want to keep values between page-calls, you could use
the Session object.
I don't know if it is a good idea to store COM objects (in Session or
elsewhere), though.

Hans Kesting
 
M

Martin Knopp

I don't know if it is a good idea to store COM objects (in Session or
elsewhere), though.

Depends on the design of the COM object. If it is designed for being used
stateful it might be OK. General rule however should be to design COM
objects stateless and then its not a good idea at all.
 
A

Alvin Bruney [MVP]

Depends on the design of the COM object. If it is designed for being used
stateful it might be OK.

It's not that it isn't a good idea, you need to be informed enough to know
when to use it optimally.

Here are some pointers:
ASP.NET 1.0 storing non-serializable objects in session fail silently with
horribly consequences later. This is repaired in 1.1 to throw an exception.

COM objects stored in session must be serializable for out of proc mode. No
real serialization takes place for in-proc.

Serialization is expensive for COM objects because they aren't basic types
which requires the use of the binary serializer instead of a highly
optimized internal serializer for out of proc mode and state server.

There's also the age old problem of circular references and COM clean-up
issues with in-proc/out-of proc scenarios to be mindful of. But the
situation is much improved from classic ASP.
 

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,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top