{"id":103868,"date":"2020-06-15T07:00:00","date_gmt":"2020-06-15T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103868"},"modified":"2020-06-15T07:18:21","modified_gmt":"2020-06-15T14:18:21","slug":"20200615-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200615-00\/?p=103868","title":{"rendered":"Error 0x80131040 &#8220;The located assembly&#8217;s manifest definition does not match the assembly reference&#8221; when I use a Windows Runtime class written in C# from my C++\/WinRT project"},"content":{"rendered":"<p>So you&#8217;re writing your C++\/WinRT project, and everything is going swimmingly. There&#8217;s some code written in C# that you&#8217;d rather not port to C++\/WinRT, just use as-is. &#8220;No problem,&#8221; you say. &#8220;I can just package the C# code as a Windows Runtime class, and then I can use the inter-language features of the Windows Runtime to allow the C# code to be consumed by C++\/WinRT.&#8221;<\/p>\n<p>You add the C# code as a project alongside your C++\/WinRT project, you add a reference from the C++\/WinRT project to the C# project, and everything builds: It&#8217;s a miracle!<\/p>\n<p>Except that when you actually try to use the C#-written Windows Runtime class, you get error 0x80131040: The located assembly&#8217;s manifest definition does not match the assembly reference.<\/p>\n<p>What&#8217;s going on, and more importantly, how do I fix it?<\/p>\n<p>Here&#8217;s what&#8217;s going on: Visual Studio checks the <i>Min version<\/i> of the C# project. This version controls which version of .NET Core and .NET Native are used. In mixed-language scenarios, such as we have here with C++\/WinRT and C#, Visual Studio defaults to .NET Core 1.1 and .NET Native 1.4. However, if the minimum version is set to Windows 10 version 1709 (Build 16299) or higher, then Visual Studio copies the .NET Core 2 libraries into the application output folder and tries to run them against .NET Core 1.1.<\/p>\n<p>That is the version mismatch that&#8217;s being reported. The mismatch is not with the version of your C# component. The mismatch is with the version of .NET Core.<\/p>\n<p>The workaround is to set your C# component&#8217;s minimum version to Windows 10 version 1703 (Build 15063) or lower: From your C# project, go to Properties, Library, and under <i>Targeting<\/i>, set the <i>Min version<\/i> to <i>Windows 10 Creators Update (10.0; Build 15063)<\/i> or lower.<\/p>\n<p>My colleague <a href=\"https:\/\/twitter.com\/jplaanstra\"> Johan Laanstra<\/a> found another workaround, which has been <a href=\"https:\/\/github.com\/asklar\/WinRTComponent\/blob\/master\/README.md\"> shared<\/a> by another colleague <a href=\"https:\/\/twitter.com\/alexsklar\"> Alexander Sklar<\/a>:<\/p>\n<blockquote class=\"q\">\n<ol>\n<li>Right click on the VCXProj file \u2192 Manage NuGet Packages.<\/li>\n<li>Search for <b>Microsoft.Net.Native.Compiler<\/b>, and install it.<\/li>\n<li>Then add the following properties to the VCXProj<\/li>\n<\/ol>\n<pre>&lt;PropertyGroup&gt;\r\n  &lt;UseDotNetNativeToolchain Condition=\"'$(Configuration)'=='Release'\"&gt;true&lt;\/UseDotNetNativeToolchain&gt;\r\n  &lt;DotNetNativeVersion&gt;2.2.3&lt;\/DotNetNativeVersion&gt;\r\n&lt;\/PropertyGroup&gt;\r\n<\/pre>\n<\/blockquote>\n<p>And it looks like <a href=\"https:\/\/a.rcad.io\/\">Carcadio Garcia<\/a> found <a href=\"https:\/\/a.rcad.io\/csharp-in-cppwinrt\"> a different, lengthier workaround<\/a>, which I haven&#8217;t tried.<\/p>\n<p><b>Bonus chatter<\/b>: <a href=\"https:\/\/github.com\/tommcdon\"> Tom McDonald<\/a> informed me that Carcadio Garcia&#8217;s workaround has not been updated for Visual Studio 16.6, which uses different directories from earlier versions of Visual Studio. Support for Visual Studio 16.6 can be found in <a href=\"https:\/\/github.com\/microsoft\/react-native-windows\/blob\/master\/packages\/microsoft-reactnative-sampleapps\/windows\/SampleAppCPP\/SampleAppCpp.vcxproj\"> this sample project<\/a>. He also shared with me some cute little tables. Here&#8217;s one of them:<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.NETCore.UniversalWindowsPlatform\/\"> Microsoft.<wbr \/>NETCore.<wbr \/>Universal\u00adWindows\u00adPlatform<\/a><\/th>\n<th>Visual Studio<\/th>\n<th>Dot\u00adNet\u00adNative\u00adVersion<\/th>\n<th>Dot\u00adNet\u00adNative\u00adShared\u00adLibrary<\/th>\n<\/tr>\n<tr>\n<td>6.2.10 (current)<\/td>\n<td>16.6<\/td>\n<td nowrap=\"nowrap\">2.2.8-rel-28605-00<\/td>\n<td>2.2.27912.00<\/td>\n<\/tr>\n<tr>\n<td>6.2.9<\/td>\n<td>16.5<\/td>\n<td nowrap=\"nowrap\">2.2.7-rel-27913-00<\/td>\n<td>2.2.27912.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There&#8217;s also an issue with where the NuGet packages are kept. They could be kept in the user&#8217;s private NuGet package store or in the Visual Studio global package store. The danger of using the user&#8217;s private NuGet package store is that the user might not have the package. The danger of using the Visual Studio global package store is that the contents of that store are not contractual and can change at the next update.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>NugetPath<\/th>\n<th>Visual Studio<\/th>\n<th>Sample project<\/th>\n<\/tr>\n<tr>\n<td>$(ProgramFiles)\\<wbr \/>Microsoft SDKs\\<wbr \/>UWP\u00adNuGet\u00adPackages<\/td>\n<td>\u2265 16.6<\/td>\n<td><a href=\"https:\/\/github.com\/tommcdon\/NetNativeSamples\/blob\/Dev16_6_Validation\/CPP-CSharp-V2\/CPP-CSharp-V2\/CPP-CSharp-V2.vcxproj\">Sample<\/a><\/td>\n<\/tr>\n<tr>\n<td>$(USERPROFILE)\\<wbr \/>.nuget\\<wbr \/>packages<\/td>\n<td>\u2264 16.5<\/td>\n<td><a href=\"https:\/\/github.com\/tommcdon\/NetNativeSamples\/blob\/master\/CPP-CSharp-V2\/CPP-CSharp-V2\/CPP-CSharp-V2.vcxproj\">Sample<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>One trick is to create a blank C# UWP app targeting the matching version of the <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.NETCore.UniversalWindowsPlatform\/\"> Microsoft.<wbr \/>NETCore.<wbr \/>Universal\u00adWindows\u00adPlatform<\/a> meta-package and build it. That will restore all of the packages into the user&#8217;s private package store so that other projects can consume them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Version numbers strike again.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103868","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Version numbers strike again.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103868"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103868\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}