Calling a .NET ServicedComponent from VB6

I had some interesting problems this week. I have a series of .NET ServicedComponents that were hung up in the COM+. The client, written in VB6, calls a ServicedComponent via a COM interop multiple times, everything seems to work. However, the number of objects and activations increase with the number of calls. COM+ seems to keep the references active.

Apprently, after Googling the problem, it looks like it’s the client’s responsibility to call the overridden Dispose() method (from IDisposable) to have COM+ deactivate the object and destroy it. However, in the VB6 client, the interop on ServicedComponent occurs on a user-defined interface. The IDisposable interface is not available, so the client cannot explicitly call Dispose(). I personally think that this is a COM interop bug. So what can I do?

Well, lucky for me that my interop interface is very simple:

...
public interface ISomething {

 void DoCommand(string command);
}
...

I only expose one method in the interface, so I tried this little trick suggested by my friend Josh.

..
public MyComponent : ServicedComponent, ISomething
{
...

 public void DoCommand(string command) {

  ...
  ...

  // Final line:
  this.Dispose();
 }
}

That’s right. Self-destruction. For a simple, one-method interface, it works well. It gets more complicated after that. However, this method will need to work until Microsoft fixes the COM interop.



%d bloggers like this: