views:

270

answers:

1

Hi,

It is my understanding that for a VB6 COM object when it goes out of scope Class_Terminate is immediately called on the object to allow it to clean up.

Is it possible to have that same functionality for a .NET object that is being called by COM?

The background to the question is based on the MSDN article: http://msdn.microsoft.com/en-us/library/aa479313.aspx

This replaces the Session object in an ASP page with a custom object that serializes the Session information back to a database as soon as the page finishes by utilising Class_Terminate.

I want to implement the object in .NET, however as the GC won't call the finalizer immediately it is a bit of a problem, as I don't want to have to wait for the object to be cleaned up before being serialized back into the database.

I could just implement IDisposiable and manually call Dispose() when finished with the Session, but that would require altering every existing ASP classic page rather than just being able to include a few lines in a standard include file.

So is there a way to automatically call the code to do the work that is required when the page has finished? :)

+3  A: 

Delegation. Wrap the .NET COM class in a VB6 Com Class that is a shell and in the Class_Terminate event of the wrapper call dispose.

If you don't want to reimplement the entire interface in the VB6 COM Class then you could do something like

Public MyNETCOMObject as MyNETCOMObject

Private Class_Initialize()
  Set MyNETCOMObject = New MyNetCOMObject
End Sub

Private Class_Terminate()
  If Not MyNETCOMObject = Nothing then MyNETCOMObject.Dispose
  Set MyNETCOMObject = Nothing
End Sub
RS Conley