724

I am creating a web application on the .NET 4.0 framework (beta2) in C#.

When I try to use a assembly called "ActiveHomeScriptLib", I get the following error:

Interop type 'ActiveHomeScriptLib.ActiveHomeClass' cannot be embedded. Use the applicable interface instead.

When I change the framework to version 3.5, I don't have any errors.

What is an Interop Type and why does this only occur when I use the 4.0 framework?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jan
  • 9,858
  • 7
  • 26
  • 33
  • I've found this article very helpful for resolving interop/PIA issues. http://blogs.msdn.com/b/vbteam/archive/2010/06/11/troubleshooting-errors-when-embedding-type-information-doug-rothaus.aspx – GilesDMiddleton Jul 29 '15 at 15:54

10 Answers10

1159

.NET 4.0 allows primary interop assemblies (or rather, the bits of it that you need) to be embedded into your assembly so that you don't need to deploy them alongside your application.

For whatever reason, this assembly can't be embedded - but it sounds like that's not a problem for you. Just open the Properties tab for the assembly in Visual Studio 2010 and set "Embed Interop Types" to "False".

EDIT: See also Michael Gustus's answer, removing the Class suffix from the types you're using.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    Unfortunately, this sounds like just what I need, but this property doesn't seem to be available anymore. – Dave Jun 23 '11 at 20:34
  • 147
    I was looking in the Project's property page instead of the right-click | Properties on the effected DLL in the References pane. – justSteve Feb 16 '12 at 03:35
  • 5
    Wouldn't it make more sense to do as the error says and "use the applicable interface"? I had this error (from a different class) and was able to instantiate an interface which had that class specified as its CoClass attribute, and it worked. As in Michael Gustus' answer below, the interface for BlahClass was just called Blah, which seems to be the standard convention. – Tim Goodman Apr 19 '12 at 16:50
  • 1
    A great thing about embedding is that the Interop assembly can remain CopyLocal=False, as you don't need it at runtime. – Schmuli May 07 '12 at 13:00
  • @TimGoodman for me "applicable interface" was not working, but setting above mentioned `embed interop types` property to `false` did the trick. In my case - I was working with `Microsoft.Office.Interop.Excel` library and needed to access Workbook object. Using it's interface `Workbook` (btw. naming convention...) was not an option - I received `COM object`, not the desired `Microsoft.Office.Interop.Excel.WorkbookClass` – Prokurors Sep 09 '16 at 12:24
  • @Prokurors You often need an explicit cast from "object" to the desired type – NeXuS Jul 04 '17 at 07:51
  • For a strange reason the reference property changed and I was getting this error. Thanks this saved me days of trying to figure out the problem – greektreat Apr 26 '23 at 18:38
533

In most cases, this error is the result of code which tries to instantiate a COM object. For example, here is a piece of code starting up Excel:

Excel.ApplicationClass xlapp = new Excel.ApplicationClass();

Typically, in .NET 4 you just need to remove the 'Class' suffix and compile the code:

Excel.Application xlapp = new Excel.Application();

An MSDN explanation is here.

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
Michael Gustus
  • 5,339
  • 1
  • 14
  • 5
  • 16
    +1 I believe this is what the error message is actually telling you to do when it says "use the applicable interface". Note that Excel.Application is an interface (despite the fact that it can be instantiated with the new keyword, similar to the situation described here: http://stackoverflow.com/questions/6960910/what-does-the-c-sharp-coclass-attribute-do ) – Tim Goodman Apr 19 '12 at 16:58
  • _"Embed Interop Types"_ to **"False"** or **"True"** ? – Kiquenet Mar 25 '15 at 11:47
  • 1
    @Kiquenet if you follow the advice here you can set the 'Embed Interop Types' back to True, or at least that worked ok for me – Sam Holder Sep 03 '15 at 11:12
132

Like Jan It took me a while to get it .. =S So for anyone else who's blinded with frustration.

  • Right click the offending assembly that you added in the solution explorer under your project References. (In my case WIA)
  • Click properties.
  • And there should be the option there for Embed Interop Assembly.
  • Set it to False
gideon
  • 19,329
  • 11
  • 72
  • 113
  • 15
    Still struggling till I realised you had to **right-click the interop assembly** under the project References in Solution Explorer, **NOT** the assembly you're building! – paytools-steve Jul 01 '11 at 16:06
  • 6
    Now, (ten years later) this option is called "Embed Interop Types" – David Foley Mar 06 '19 at 19:54
46

Here's where to set the Embed Interop in Visual Studio 2012

enter image description here

nobody
  • 10,892
  • 8
  • 45
  • 63
35

Expanding on Jon's correct answer.

The problem here is that your are combining the new "Embed Interop Types" (or NoPIA) feature with use of a class type. The "Embed Interop Types" feature works by essentially statically linking in all of the types from a PIA (Primary Interop Assembly) into the referencing assembly removing the overhead of deploying it.

This feature works great for most types in a PIA but it does have restrictions. One of them is that you cannot embed classes (it's a servicing issue). Misha has a detailed blog article on why this is not allowed

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
15

Got the solution

Go to references right click the desired dll you will get option "Embed Interop Types" to "False" or "True".

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
Navdeep
  • 151
  • 1
  • 2
  • 1
    This also worked for VS2015 c# with .net using PP_COM_Wrapper; given in http://www.cypress.com Cypress Semiconductor Corporation C# Lib example. Setting to False got rid of the error. – user3564895 Jun 01 '16 at 16:45
9

I ran into this issue when pulling down a TFS project to my local machine. Allegedly, it was working fine on the guy's machine who wrote it. I simply changed this...

WshShellClass shellClass = new WshShellClass();

To this...

WshShell shellClass = new WshShell();

Now, it is working like a champ!

Zach
  • 640
  • 1
  • 6
  • 16
  • 1
    This approach worked for me as well! In my case, I was debugging to find where the value I needed was, right-clicked and selected "copy expression". What was given to me was "...HTMLDocumentClass..." Removing the text "Class" from it solved the issue for me. – majestzim Feb 10 '16 at 21:22
3

I had same problem in VB.NET 2013 with Office 2007, and this solved it:

VS 2013 VB.NET Project > Props > Refs > Microsoft Word 12.0 Object Lib > Embed Interop Types: change True to False

Doug Null
  • 7,989
  • 15
  • 69
  • 148
1

http://digital.ni.com/public.nsf/allkb/4EA929B78B5718238625789D0071F307

This error occurs because the default value is true for the Embed Interop Types property of the TestStand API Interop assembly referenced in the new project. To resolve this error, change the value of the Embed Interop Types property to False by following these steps: Select the TestStand Interop Assembly reference in the references section of your project in the Solution Explorer. Find the Embed Interop Types property in the Property Browser, and change the value to False

Ramezani r
  • 161
  • 1
  • 7
1

Visual Studio 2017 version 15.8 made it possible to use the PackageReferencesyntax to reference NuGet packages in Visual Studio Extensibility (VSIX) projects. This makes it much simpler to reason about NuGet packages and opens the door for having a complete meta package containing the entire VSSDK.

Installing below NuGet package will solve the EmbedInteropTypes Issue.

Install-Package Microsoft.VisualStudio.SDK.EmbedInteropTypes

Rahul
  • 2,431
  • 3
  • 35
  • 77