3

我想使用Windows 10物聯網核心C++後臺應用程序(基於the MSFT IoT templates)。Windows 10物聯網核心C++後臺應用程序引用C#運行時組件

我的場景涉及創建一個利用現有託管(C#)運行時組件的本機(C++)後臺應用程序。我可以在Visual Studio中創建這樣一個解決方案,它可以很好地編譯和部署到物聯網設備。

然而,當我運行應用程序時,我看到了運行時異常這樣隨時隨地的管理組件用於:Windows運行時的承諾

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 

部分是語言(C++,C#的互操作, JS,VB)...這種情況在標準的UWP應用程序代替物聯網後臺應用程序的情況下工作得很好。

這種情況如何適用於後臺應用程序?

回答

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庫部署後臺應用程序和本地代碼可以成功訪問託管組件。

相關問題