title: Issues with COM Interop and .NET 3.5 - different behaviors in Debug and Release mode
body: <p>We have been troubleshooting the following and are looking for some fresh ideas or angles of attack.</p>
  
  <p>First scenario: </p>
  
  <p>We have an ASP.NET web application - .NET 3.5, C#, built targeting x86 - running on IIS7/Windows Server 2008 R2 (64 bit).  The ASP.NET web application runs under the default application pool with 32 bit applications enabled. </p>
  
  <p>This web application leverages some legacy components written in VB6 and built as ActiveX DLLs. The DLLs are registered using regsvr32. References to these components are added to the web application project (Visual Studio 2008) and we're using the generated Interop classes.</p>
  
  <p>When the web application is built in Release mode, errors are thrown by the legacy code fairly deep in the subroutine stack every time a call is made to those legacy VB6 components. It's usually something like:</p>
  
  <p>ERROR 91 Object variable or With block variable not set [ComponentName:ClassName.cls:MethodName]</p>
  
  <p>When the same web application code is built in Debug mode instead, the same errors occur but they are intermittent. If the same action is repeated in the web application 10 times (same inputs, everything else being equal) we'll see the problem approximately once.</p>
  
  <p>Second scenario:</p>
  
  <p>In addition to the web application, we have a .NET 3.5 C# application built targeting x86 that runs as a Windows service on Windows Server 2008 R2 (64 bit). This service makes calls to an ActiveX EXE that is related to the DLLs used by the website (common legacy heritage and dependencies). The ActiveX EXE is registered using the /regserver switch and a reference added to the .NET project. The generated Interop classes are used. As is the nature of ActiveX EXEs, method calls execute asynchronously in their own thread and use events to notify the caller of completion.</p>
  
  <p>If the .NET application is built in Release mode, method calls to the EXE do not raise errors (as far as I can see), but the event that should be raised in the ActiveX EXE to indicate success is not received by the .NET application.</p>
  
  <p>If the same .NET application code is built in Debug mode, the event is raised by the EXE and received/handled by the .NET application.</p>
  
  <p>I've posting these issues together because they both change with debug versus release build and my hunch is that the causes are related. Currently we're focusing on potential issues with garbage collection and the out-of-process work done by the ActiveX EXE and threading issues in the web application.  Any ideas about areas we should investigate would be welcome. </p>