我尝试使用Windows 10 IoT核心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应用程序代替物联网背景应用程序。
此方案如何适用于后台应用程序???
答案 0 :(得分:3)
处理后台应用程序的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库与Background Application一起部署,本机代码可以成功访问托管组件。