Windows 10 IoT 核心 C++ 后台应用程序引用 C# 运行时组件

Windows 10 IoT Core C++ Background Application referencing a C# Runtime Component

我正在尝试使用 Windows 10 IoT Core C++ 后台应用程序(基于 the MSFT IoT templates)。

我的方案涉及创建一个利用现有托管 (C#) 运行时组件的本机 (C++) 后台应用程序。我可以在 Visual Studio 中创建这样的解决方案,它可以很好地编译和部署到 IoT 设备。

但是,当我 运行 应用程序时,每当使用托管组件时,我都会看到 运行 像这样的时间异常:

Exception thrown at 0x76C92052 in backgroundTaskHost.exe: Microsoft C++ 
exception: Platform::ClassNotRegisteredException ^ at memory location 
0x02B0F4A8. HRESULT:0x80040154 Class not registered

WinRT information: Class not registered

Stack trace:
[External Code]
backgroundapplicationcpp.dll!BackgroundApplicationCpp::StartupTask::
[Windows::ApplicationModel::Background::IBackgroundTask]::Run
(Windows::ApplicationModel::Background::IBackgroundTaskInstance ^ 
taskInstance) Line 13

Windows 运行时的部分承诺是语言(C++、C#、JS、VB)的互操作......这个场景适用于标准 UWP 应用程序物联网后台应用程序。

这种情况如何适用于后台应用程序???

Visual Studio 目标系统处理后台应用程序的部分将项目中的每个库都视为与后台应用程序相同的语言 (C++)。

在这种情况下,托管运行时组件被视为 C++ 组件。因此,.NET 库未包含在部署中。

Visual Studio 的下一个版本应该包含对此的修复,但在那之前,我将其添加到我的后台应用程序的 vcxproj:

<!-- Workaround for bug in MSBuild regarding Native Background Applications referencing Managed Conponents -->
<PropertyGroup>
  <CopyNuGetImplementations>true</CopyNuGetImplementations>
  <NuGetRuntimeIdentifier>win10-$(PlatformTarget.ToLower())</NuGetRuntimeIdentifier>
</PropertyGroup>
<Target Name="_LocalResolvePrimaryProjectWinmdFiles" BeforeTargets="BeforeGenerateAppxManifest" AfterTargets="_ResolvePrimaryProjectWinmdFiles" Condition="'$(OutputType)' != 'exe' and '$(AppxPackage)' == 'true' AND '$(ContainsStartupTask)' == 'true'">
  <ItemGroup>
    <_AppxWinmdFilesToHarvest Remove="@(_AppxWinmdFilesToHarvest)" />
    <_AppxWinmdFilesToHarvest Include="@(PackagingOutputs)" Condition="'%(PackagingOutputs.Extension)' == '.winmd' and '%(PackagingOutputs.ProjectName)' == '$(ProjectName)' and '%(PackagingOutputs.ResolvedFrom)' != 'GetSDKReferenceFiles'">
      <!-- This covers the Managed Background Application winmd which does NOT have a WinMDFileType value set -->
      <ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == ''">WindowsRuntime 1.4;CLR v4.0.30319</ImageRuntime>
      <!-- This covers the C++ Background Application winmd which does NOT have a WinMDFileType value set -->
      <ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '@(Language)' == 'C++'">WindowsRuntime 1.4</ImageRuntime>
      <!-- This covers Managed Windows Runtime Component winmds -->
      <ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '%(PackagingOutputs.WinMDFileType)' == 'Managed'">WindowsRuntime 1.4;CLR v4.0.30319</ImageRuntime>
      <!-- This covers Native Windows Runtime Component winmds -->
      <ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '%(PackagingOutputs.WinMDFileType)' == 'Native'">WindowsRuntime 1.4</ImageRuntime>
      <!-- This covers Native Windows Runtime Component winmds for DynamicLibrary projects -->
      <ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '%(PackagingOutputs.ProjectType)' == 'DynamicLibrary'">WindowsRuntime 1.4</ImageRuntime>
      <!-- This provides an override -->
      <ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' != ''">$(PrimaryProjectWinmdImageRuntimeOverride)</ImageRuntime>
    </_AppxWinmdFilesToHarvest>
  </ItemGroup>
</Target>

使用该代码块,.NET 库随后台应用程序一起部署,本机代码可以成功访问托管组件。