【问题标题】:Windows 10 IoT Core C++ Background Application referencing a C# Runtime Component引用 C# 运行时组件的 Windows 10 IoT Core C++ 后台应用程序
【发布时间】:2017-01-11 01:56:44
【问题描述】:

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

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

但是,当我运行应用程序时,只要使用托管组件,我就会看到类似这样的运行时异常:

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 应用程序代替 IoT 后台应用程序即可正常工作。

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

【问题讨论】:

    标签: uwp windowsiot background-application windows-iot-core-10


    【解决方案1】:

    处理后台应用程序的 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 库与后台应用程序一起部署,并且本机代码可以成功访问托管组件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2019-07-21
      • 2019-06-04
      相关资源
      最近更新 更多