Subscribe Now: Feed Icon

Monday, February 14, 2011

Build Error: Using COM References in .Net 4.0

I found that several of my team’s projects had .Net 3.5 by mistake so I upgraded them and got this error:

Member 'Project.CadWorkspaceUtils.GetGeometries()' from assembly 'c:\Projects\Company\GIS\Framework\Server\Bin\Company.GIS.Core.Esri.dll' cannot be used across assembly boundaries because it contains a type which has a generic type parameter that is an embedded interop type.   

(it took me a while to find the cause because I also upgraded the ESRI WPF/Silverlight API references and I thought that something got removed from the framework)

CadWorkspaceUtils.GetGeometries() returns List<IGeometry> that are in a CAD file, that class sits in a .Net 3.5 project (I can’t upgrade that one yet because of constraints outside of my team).

The error was caused because in .Net 4.0 referencing COM Dlls has been upgraded with this option:

com-reference-dotnet-4

In .Net 4.0 the default is true whereas in .Net 3.5 the option does not exist. The error is fixed easily by changing the “Embed Interop Types” in the reference to false in the .Net 4.0 project.

 

But what does that option mean?

Type Embedding and Type Equivalence

Another major concern related to deployment of Office customizations was deployment of Primary Interop Assemblies (PIAs). Before .NET Framework 4 if the customization only used some of the interfaces and methods from the Interop Assemblies, the complete set of the Primary Interop Assemblies still had to be deployed. .Net Framework 4 addresses this in very elegant way. The C# and VB compilers provide a /link switch that embeds the types referenced from the Primary Interop Assemblies into the application assembly itself. , Type Equivalence in the CLR can understand these embedded types and link them to the appropriate types at runtime. With this the customization does not require deployment of the Primary Interop Assemblies on end-user computers.

Taken from here.

It means that the assembly types are inserted into the project assembly (without the original Dll).

As far as I can understand it the original type does not actually equals the embedded type – causing the build error.

 

Resources:

Why Should I Upgrade from .Net Framework 3.5 to .Net Framework 4 ? (Navneet Gupta)

CLR and DLR and BCL, oh my! - Whirlwind Tour around .NET 4 (and Visual Studio 2010) Beta 1

 

Keywords: build error, com, reference

IceRocket Tags: ,,