C
Chris Puncher
Hi.
I have a RCW class that was generated by the VS.NET2003 IDE when I added a
COM dll reference to my project. This all works fine when I call methods on
it. My initial problem is that in ASP.NET I would like to add this to the
Session object whilst using SQLServer as the session storage. Unfortunately,
as this required the RCW class to be serializable, this won't work, and
throws an exception saying you can't Serialize the Session state if it
contains objects that are MarshalByRef and the session state mode is
SQLServer.
So question 1 - Is there an easy way to make an RCW class serializable? (I'm
expecting this to be "No", but if you don't ask you don't get)
Question 2 - My COM dll is capable of storing its own state, via its
implementation of IPersistStream. After a lot of Googling I've added C#
definitions of IPersist and IPersistStream. If I have a class AAA which
contains an instance of MyLib::IRCWClass which is marked as
NonSerializable, then I've set AAA to implement ISerializable, and in
GetObjectData() I have
MemoryStream memstream = new MemoryStream();
ComStream cstream = new ComStream(memstream);
IPersistStream pStream = (IPersistStream)m_RCWClass;
if ( pStream != null )
pStream.Save( cstream, false );
else
Trace.WriteLine( "No IPersistStream" );
This throws the same exception when it calls Save. However if I change the
third line to
IPersistStream pStream = m_RCWClass as IPersistStream;
This also throws the same exception. Is there any way to gain access to the
IPersistStream methods, and then serialize the contents of my stream?
Question 3 - As an alternative approach, I've tried to get the bytes out of
the IPersistStream another way, with a view to storing this byte array
separately in the Session object. A first attempt is
IPersistStream p = m_AAAClass.RCWClass as IPersistStream;
if ( p != null )
{
System.IO.MemoryStream memstream = new System.IO.MemoryStream();
ComStream cstream = new ComStream(memstream);
if ( p != null )
{
p.Save( cstream, false );
System.IO.MemoryStream mem = cstream.stream as
System.IO.MemoryStream;
return mem.GetBuffer();
}
else
return null;
}
return null;
This time the pointer to IPersistStream is null the first time, but after
that always throws a COMException with the message "The server threw an
exception". Am I barking up the wrong tree?
Fundamentally wanting to store an RCW class in the Session object doesn't
seem to be a strange thing to want to do, but after Googling myself into a
frenzy, it seems that very few people have asked about it. Those that have
have been told to set the sesseion state to InProc, but this isn't really an
option, as this means you can't run on a web farm. Any ideas please?
Cheers,
Chris
I have a RCW class that was generated by the VS.NET2003 IDE when I added a
COM dll reference to my project. This all works fine when I call methods on
it. My initial problem is that in ASP.NET I would like to add this to the
Session object whilst using SQLServer as the session storage. Unfortunately,
as this required the RCW class to be serializable, this won't work, and
throws an exception saying you can't Serialize the Session state if it
contains objects that are MarshalByRef and the session state mode is
SQLServer.
So question 1 - Is there an easy way to make an RCW class serializable? (I'm
expecting this to be "No", but if you don't ask you don't get)
Question 2 - My COM dll is capable of storing its own state, via its
implementation of IPersistStream. After a lot of Googling I've added C#
definitions of IPersist and IPersistStream. If I have a class AAA which
contains an instance of MyLib::IRCWClass which is marked as
NonSerializable, then I've set AAA to implement ISerializable, and in
GetObjectData() I have
MemoryStream memstream = new MemoryStream();
ComStream cstream = new ComStream(memstream);
IPersistStream pStream = (IPersistStream)m_RCWClass;
if ( pStream != null )
pStream.Save( cstream, false );
else
Trace.WriteLine( "No IPersistStream" );
This throws the same exception when it calls Save. However if I change the
third line to
IPersistStream pStream = m_RCWClass as IPersistStream;
This also throws the same exception. Is there any way to gain access to the
IPersistStream methods, and then serialize the contents of my stream?
Question 3 - As an alternative approach, I've tried to get the bytes out of
the IPersistStream another way, with a view to storing this byte array
separately in the Session object. A first attempt is
IPersistStream p = m_AAAClass.RCWClass as IPersistStream;
if ( p != null )
{
System.IO.MemoryStream memstream = new System.IO.MemoryStream();
ComStream cstream = new ComStream(memstream);
if ( p != null )
{
p.Save( cstream, false );
System.IO.MemoryStream mem = cstream.stream as
System.IO.MemoryStream;
return mem.GetBuffer();
}
else
return null;
}
return null;
This time the pointer to IPersistStream is null the first time, but after
that always throws a COMException with the message "The server threw an
exception". Am I barking up the wrong tree?
Fundamentally wanting to store an RCW class in the Session object doesn't
seem to be a strange thing to want to do, but after Googling myself into a
frenzy, it seems that very few people have asked about it. Those that have
have been told to set the sesseion state to InProc, but this isn't really an
option, as this means you can't run on a web farm. Any ideas please?
Cheers,
Chris