{"id":823,"date":"2010-06-11T10:17:00","date_gmt":"2010-06-11T10:17:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2010\/06\/11\/troubleshooting-errors-when-embedding-type-information-doug-rothaus\/"},"modified":"2024-07-05T12:41:02","modified_gmt":"2024-07-05T19:41:02","slug":"troubleshooting-errors-when-embedding-type-information-doug-rothaus","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/troubleshooting-errors-when-embedding-type-information-doug-rothaus\/","title":{"rendered":"Troubleshooting Errors When Embedding Type Information (Doug Rothaus)"},"content":{"rendered":"<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">Visual Studio 2010 has a new feature,<b> Embed Interop Types<\/b>, that can simplify application deployment and solve those pesky issues that can arise when using COM Interop and Primary Interop Assemblies (PIAs). If you&rsquo;ve ever had to ship multiple versions of an application that automates Microsoft Office where the only difference between your published versions is the version of the PIA (to match different Office versions), then this feature is for you.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">You enable type embedding when you reference an assembly. In Visual Studio, you set the <b>Embed Interop Types property <\/b>to <b>true<\/b> (the default). The command-line compiler equivalent is the new <b>\/link<\/b> compiler option. You can read all about the feature here: <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd264728.aspx\"><span style=\"font-size: small\">\/link compiler option (C#)<\/span><\/a><span style=\"font-size: small\">, <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd293566.aspx\"><span style=\"font-size: small\">\/link compiler option (VB)<\/span><\/a><span style=\"font-size: small\">.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">There are also a couple of examples available in the help documentation:<\/span><\/p>\n<ul>\n<li class=\"MsoListParagraphCxSpFirst\" style=\"line-height: normal;text-indent: -0.25in\"><span style=\"font-family: Symbol\"><span style=\"font-size: small\"><span style=\"line-height: normal;font-variant: normal;font-style: normal;font-family: 'Times New Roman';font-weight: normal\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee317478.aspx\"><span style=\"font-size: small\">Walkthrough: Embedding Type Information from Microsoft Office Assemblies (C# and Visual Basic)<\/span><\/a><\/li>\n<li class=\"MsoListParagraphCxSpLast\" style=\"line-height: normal;text-indent: -0.25in\"><span style=\"font-family: Symbol\"><span style=\"font-size: small\"><span style=\"line-height: normal;font-variant: normal;font-style: normal;font-family: 'Times New Roman';font-weight: normal\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd409610.aspx\"><span style=\"font-size: small\">Walkthrough: Embedding Types from Managed Assemblies (C# and Visual Basic)<\/span><\/a><span style=\"font-size: small\"> (it&rsquo;s not just for COM Interop)<\/span><\/li>\n<\/ul>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">When you embed type information, you can run into some confusing errors. Here are the most common errors and information on the cause and resolution.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3>VBC40059 (VB), CS1762 (C#)<\/h3>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-family: 'Courier New';color: #943634;font-size: 10pt\">A reference was created to embedded interop assembly &#8216;&lt;assembly1&gt;&#8217; because of an indirect reference to that assembly from assembly &#8216;&lt;assembly2&gt;&#8217;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">This error can surprise you. Because type the <b>Embed Interop Types<\/b> property is set to <b>true<\/b> by default, you are embedding type information and may not realize it. This error will occur when you have references to two different assemblies. For the first assembly reference, you have set the <b>Embed Interop Types<\/b> property to <b>True<\/b>. For the second you have set the <b>Embed Interop Types<\/b> property to <b>False<\/b>. The error occurs in this case because the second assembly also references the first assembly. Now you have a situation where your application instructs the compiler to both embed type information from the first assembly, and to not embed type information from the first assembly, via the second assembly.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">To resolve the issue, you need to set the <b>Embed Interop Types<\/b> property for both assembly references to the same value; <b>true<\/b> to embed type information, <b>false<\/b> to get type information from the PIA.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: 12pt\">&nbsp;<\/span><\/p>\n<h3>VBC 31539 (VB), CS1748 (C#)<\/h3>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-family: 'Courier New';color: #943634;font-size: 10pt\">Cannot find the interop type that matches the embedded interop type &#8216;&lt;type&gt;&#8217;. Are you missing an assembly reference?<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">This method is similar to the previous error in that it occurs if one assembly embeds type information and another does not. In this case, you have an assembly, <span style=\"font-family: 'Courier New'\">assembly1<\/span>, that references a PIA assembly with <b>Embed Interop Types<\/b> is set to <b>true<\/b>. <span style=\"font-family: 'Courier New'\">Assembly1<\/span> then exposes a type from the PIA assembly, for example as the return type from a method or property. Another assembly, <span style=\"font-family: 'Courier New'\">assembly2<\/span>, references <span style=\"font-family: 'Courier New'\">assembly1<\/span> and makes use of the embedded type. The error occurs if <span style=\"font-family: 'Courier New'\">assembly2<\/span> does not also reference the PIA assembly, and therefore cannot locate the embedded type information.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">To resolve the issue, you need to add a reference from the second assembly to the PIA assembly and set the <b>Embed Interop Types<\/b> property to <b>true<\/b>. Thus, both assemblies will have a reference to the PIA assembly, with type information embedded, and both can utilize the embedded type.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: 12pt\">&nbsp;<\/span><\/p>\n<h3>VBC36924 (VB), CS1772 (C#)<\/h3>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-family: 'Courier New';color: #943634;font-size: 10pt\">Type &#8216;&lt;type&gt;&#8217; cannot be used across assembly boundaries because a type in its inheritance hierarchy has a generic type parameter that is an embedded interop type.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">This error occurs in the same scenario as VBC 31539 (VB) and CS1748 (C#).&nbsp; You have an assembly, <span style=\"font-family: 'Courier New'\">assembly1<\/span>, that references an assembly with <b>Embed Interop Types<\/b> is set to <b>true<\/b>. <span style=\"font-family: 'Courier New'\">Assembly1<\/span> then exposes a type and another assembly, <span style=\"font-family: 'Courier New'\">assembly2<\/span>, utilizes the exposed type. The error occurs if the type that is exposed is a generic, and the generic parameter is of an embedded type.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal\"><span style=\"font-size: small\">Unfortunately, this is a limitation of the Common Language Runtime (CLR). If you are able to, you can resolve the issue by modifying the assembly that exposes the embedded type as a generic parameter. You can either modify the references to the PIA assembly to set <b>Embed Interop Types<\/b> to <b>false<\/b> and thus reference that &nbsp;PIA assembly &nbsp;that contains the COM type information. Or, as the author of the library you can expose an interface with an embedded type as a generic parameter. For example, if you encounter this error when calling a method that returns a generic list such as <b>List(Of Excel.Application) <\/b>(in C# <b>List&lt;Excel.Application&gt;<\/b>), then you could resolve the issue by modifying the method to return <b>IList(Of Excel.Application)<\/b> (in C# <b>IList&lt; Excel.Application &gt;<\/b>).<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 2010 has a new feature, Embed Interop Types, that can simplify application deployment and solve those pesky issues that can arise when using COM Interop and Primary Interop Assemblies (PIAs). If you&rsquo;ve ever had to ship multiple versions of an application that automates Microsoft Office where the only difference between your published versions [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[195],"tags":[59,156,167],"class_list":["post-823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-basic","tag-doug-rothaus","tag-troubleshooting","tag-vb2010"],"acf":[],"blog_post_summary":"<p>Visual Studio 2010 has a new feature, Embed Interop Types, that can simplify application deployment and solve those pesky issues that can arise when using COM Interop and Primary Interop Assemblies (PIAs). If you&rsquo;ve ever had to ship multiple versions of an application that automates Microsoft Office where the only difference between your published versions [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}