Embedded Interops Problem and Workaround

Apr 4, 2012 at 3:05 PM

VS2010 provides the ability to "embed interop types".  This creates problems for the Com dynamic proxies as embedded interops use Interfaces; e.g., the IApplication interface for Excel.  The specific problem is that the Dynamic Proxy can't locate implementations for the methods contained in interfaces when using Excel.Application app = new Exec.Application(). The solution is to not embed the interop types and use Excel.Application app = new Exec.ApplicationClass().

The problem can be reproduced as follows:

  1. Ensure the embed interop types set to True under the properties for the  Interop.Excel assembly.
  2. Create an instance of Excel: Excel.Application app = new Exec.Application().  // This will use the the interop interface for construction.
  3. Set the visibility: app.Visibility = true. // This will throw an exception to the effect "method 'set_Visibility' not found".

The solution is:

  1. Ensure the embed interop types set to False under the properties for the Interop.Excel assembly.
  2. Create an instance of Excel: Excel.Application app = new Exec.ApplicationClass(). // This will use a class for construction and not the interop interface.
  3. Set the visibility: app.Visibility = true. // No exception thrown.  The Dynamic Proxy can find the method implementation in the class.
Coordinator
Apr 5, 2012 at 12:17 AM

Thanks for the report, you are correct. VSTO Contrib does not play well with the new embed interop types feature. My install.ps1 in the NuGet packages actually turn off the feature when the package is installed.

I have been thinking about the dynamic proxy and helpers becoming a source code dropin, rather than a dll. This will solve the embed interop issues. 

If you have any suggestions how I can solve it from the VSTO Contrib side, i am happy to hear them :)

Oct 10, 2013 at 7:35 AM
Edited Oct 10, 2013 at 10:25 AM
Have you solved this problem?

It would be great to use embedding if that's possible:
  • this would allow to target multiple versions of Office application with a single add-in;
  • also, this would likely make supporting multiple Office versions easier for VSTO Contrib itself, as it could embed COM interops without forcing add-in code to take dependency on any specific version.
Oct 10, 2013 at 11:04 AM