打包MSIX时AppService出现AppUnavailable的问题排查(附最小复现示例)
我最近碰到一个棘手的问题,先给大家梳理下我的项目结构:
/ └── MyApp.Package.wapproj → WAP项目,负责将MyApp.Win打包为MSIX └── MyApp.Win.csproj → Avalonia net8.0-windows10.0.19041.0项目 ├── MyApp.Shared.csproj → 共享的Avalonia .NET 8.0项目 └── AppService-RuntimeComponent.csproj → net8.0-windows10.0.19041.0的UWP Runtime Component,用于托管后台任务并暴露AppService
这个结构是参考WASDKSample搭建的,在Visual Studio里不管是Debug还是Release模式直接部署运行,AppService都能正常建立连接,完全没毛病。但当我用**Publish -> Create app packages...**工具把WAP项目打包成MSIX并安装后,我的AppService测试客户端连接时就会触发AppUnavailable状态。
我已经检查过生成的MSIX包,里面AppService-RuntimeComponent.dll和WinRT.Host.dll都存在,文件没丢失,那为啥VS直接部署和MSIX安装后的表现会不一样呢?
我的Package.appxmanifest内容如下:
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4" IgnorableNamespaces="uap uap3 uap4 rescap"> <Identity Name="XXXXXXXXXXXXXXXXX" Publisher="CN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" Version="3.0.1.0" /> <Properties> <DisplayName>XXXXXXXXXXXXXXXXXXXXX</DisplayName> <PublisherDisplayName>XXXXXXXXXXXXXXXXXXXX</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.19041.0" MaxVersionTested="10.0.19041.0" /> </Dependencies> <Resources> <Resource Language="x-generate"/> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="Windows.FullTrustApplication"> <uap:VisualElements DisplayName="XXXXXXXXXXXXX" Description="XXXXXXXXXXXXXXXXXXXX" BackgroundColor="transparent" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png"> <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" Square71x71Logo="Assets\SmallTile.png" Square310x310Logo="Assets\LargeTile.png"/> <uap:SplashScreen Image="Assets\SplashScreen.png" /> <uap:LockScreen BadgeLogo="Assets\BadgeLogo.png" Notification="badge"/> </uap:VisualElements> <Extensions> <uap:Extension Category="windows.appService" EntryPoint="WindowsAppService.AppService"> <uap:AppService Name="XXXXXXXX.UWP.IPC" uap4:SupportsMultipleInstances="false" /> </uap:Extension> </Extensions> </Application> </Applications> <!-- To host the BgTaskComponent, you must add this activatable class entry --> <Extensions> <Extension Category="windows.activatableClass.inProcessServer"> <InProcessServer> <Path>WinRT.Host.dll</Path> <ActivatableClass ActivatableClassId="AppService-RuntimeComponent.AppService" ThreadingModel="both"/> </InProcessServer> </Extension> </Extensions> <Capabilities> <rescap:Capability Name="runFullTrust" /> <Capability Name="internetClient" /> <DeviceCapability Name="radios" /> <DeviceCapability Name="bluetooth"/> <DeviceCapability Name="location"/> <DeviceCapability Name="serialcommunication"> <Device Id="any"> <Function Type="name:serialPort"/> </Device> </DeviceCapability> </Capabilities> </Package>
补充排查信息
Edit 1:我制作了一个最小可复现的示例项目,里面的AppService在VS Release模式直接部署时能正常工作,但打包成MSIX安装后就会触发AppUnavailable。
Edit 2:我把调试器附加到backgroundTaskHost.exe上,当客户端调用AppServiceConnection.openAsync()时,抛出了如下异常:
Exception thrown at 0x00007FF984637F9A (KernelBase.dll) in backgroundTaskHost.exe: WinRT originate error - 0x80070002 : 'The system cannot find the file specified.'
有没有朋友能帮忙分析下,VS直接部署和MSIX安装后到底存在什么差异,导致AppService无法正常工作呢?
内容来源于stack exchange

