WinUI3中如何实现AppService?UWP升级WinUI3遇AppService故障
WinUI3中使用AppService的可行性及配置要求
我正尝试将UWP应用升级至WinUI3,但无法让AppService正常工作。
已尝试的方案:
- 对可正常运行的UWP最简示例使用升级工具 - 出现
AppUnavailable错误 - 使用通用Windows项目 - 出现版本不兼容错误
- .NET 7项目 - 出现
AppUnavailable错误 - 设置
OutputType为winmdobj- 出现版本不兼容错误 - 实验性C# Windows Runtime组件 - 出现版本不兼容错误
微软关于AppService的文档十分匮乏,仅能找到UWP相关文档。
请问是否可在WinUI3中使用AppService?若可以,项目需满足哪些要求?
当前项目配置
清单配置截图

RuntimeComponent1.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0-windows10.0.22621</TargetFramework> <OutputType>Library</OutputType> <DefaultLanguage>en-US</DefaultLanguage> <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion> <AllowCrossPlatformRetargeting>false</AllowCrossPlatformRetargeting> <CsWinRTComponent>true</CsWinRTComponent> <UseWinUI>true</UseWinUI> <ApplicationManifest>app.manifest</ApplicationManifest> <EnablePreviewMsixTooling>true</EnablePreviewMsixTooling> <Platforms>x86;x64;arm64</Platforms> <PublishProfile>win10-$(Platform).pubxml</PublishProfile> <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> <OutputPath>bin\x86\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> <OutputPath>bin\x86\Release\</OutputPath> <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'"> <OutputPath>bin\ARM\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'"> <OutputPath>bin\ARM\Release\</OutputPath> <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM64'"> <OutputPath>bin\ARM64\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM64'"> <OutputPath>bin\ARM64\Release\</OutputPath> <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> <OutputPath>bin\x64\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> <OutputPath>bin\x64\Release\</OutputPath> <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants> <NoWarn>;2008</NoWarn> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.0" /> <PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.2" /> </ItemGroup> </Project>
MyAppService.cs
using System; using Windows.ApplicationModel.Background; namespace RuntimeComponent1 { public sealed class MyAppService : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { //breakpoint Console.WriteLine("Hello World"); } } }
解答
WinUI3(基于Windows App SDK)可以支持AppService,但需要遵循特定的项目结构和配置要求,你的当前配置存在几个关键问题:
1. 组件项目类型错误
你当前使用的是带WinUI的类库项目,这不是实现AppService的正确类型。WinUI3中实现AppService需要:
- 新建C# Windows Runtime Component项目(非实验性版本),目标框架选择
net7.0-windows10.0.22621或更高,且不要勾选"Use WinUI 3"(AppService属于系统后台组件,不需要WinUI依赖)。 - 移除组件项目中的
<UseWinUI>true</UseWinUI>配置,避免不必要的依赖冲突。
2. 配置项调整
- 移除
NETFX_CORE和WINDOWS_UWP预编译常量:这些是UWP专属的,WinUI3基于Windows App SDK,不需要这些定义,会导致编译冲突。 - 确保
CsWinRTComponent设为true,保证组件能正确生成WinMD格式供系统调用。 - 组件项目不需要
ApplicationManifest和EnablePreviewMsixTooling配置,这些是主应用的配置项,应移除。
3. 主应用配置
- 主应用必须是Windows App SDK的WinUI3项目(打包的桌面应用)。
- 在主应用的Package.appxmanifest中,正确声明AppService扩展:
<Extensions> <uap:Extension Category="windows.appService"> <uap:AppService Name="YourAppServiceName" /> </uap:Extension> </Extensions> - 确保主应用已引用Runtime Component项目,并且组件项目已被包含在主应用的打包项目中。
4. 版本兼容
- 升级
Microsoft.WindowsAppSDK到最新稳定版(如1.4.2),旧版本(如1.1.0)存在已知的AppService兼容性问题。 - 确保
Microsoft.Windows.CsWinRT版本与Windows App SDK版本匹配,避免版本不兼容。
5. 调试注意事项
- AppService作为后台组件,无法直接在调试器中触发断点,需要在
Run方法开头添加Debugger.Launch()来手动附加调试器:public void Run(IBackgroundTaskInstance taskInstance) { #if DEBUG System.Diagnostics.Debugger.Launch(); #endif // 你的业务逻辑 }
按以上调整后,AppService应该能在WinUI3应用中正常工作。
内容的提问来源于stack exchange,提问作者Biff MaGriff
相关产品推荐
相关产品推荐

