当桌面桥应用使用更新API时,断开网络会导致应用崩溃

When the desktop bridge application uses the update API, disconnecting the network will cause the app to crash

在断网的情况下使用StoreContext.CanSilentlyDownloadStorePackageUpdates会导致app崩溃,我尝试使用Try-Catch未能捕捉到这个异常。 另外,我在使用StoreContext.TrySilentDownloadStorePackageUpdatesAsync下载更新包时,此时断网也会导致APP也崩溃

代码片段:

[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
public bool CanSilentlyUpdate()
{
    try
    {
        //There will be an error:
        //    System.AccessViolationException :“Attempted to read or write protected memory. 
        //    This is often an indication that other memory is corrupt.”
        return StoreContext.GetDefault().CanSilentlyDownloadStorePackageUpdates;
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
        return false;
    }
}

以下是事件查看器中的错误日志:

Eventlog 1: Application: MyApp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.AccessViolationException at Windows.Services.Store.StoreContext.get_CanSilentlyDownloadStorePackageUpdates()

Eventlog 2: Faulting application name: MyApp.exe, version: 1.0.0.0, time stamp: 0x96223b82 Faulting module name: Windows.ApplicationModel.Store.dll, version: 10.0.19041.329, time stamp: 0x92fa3e59 Exception code: 0xc0000005 Fault offset: 0x0009fcb0 Faulting process id: 0x6d0c Faulting application start time: 0x01d6511c5d86de48 Faulting application path: C:\Program Files\WindowsApps\MyApp\MyApp.exe Faulting module path: C:\Windows\System32\Windows.ApplicationModel.Store.dll Report Id: d67098d6-39c7-4a02-a837-b523821c9ca2 Faulting package full name: MyApp Faulting package-relative application ID: App

Eventlog 3: Application: MyApp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: exception code c0000005, exception address 7947FCB0 Stack:

Eventlog 4: Faulting application name: MyApp.exe, version: 1.0.0.0, time stamp: 0x96223b82 Faulting module name: Windows.ApplicationModel.Store.dll, version: 10.0.19041.329, time stamp: 0x92fa3e59 Exception code: 0xc0000005 Fault offset: 0x0009fcb0 Faulting process id: 0x36a0 Faulting application start time: 0x01d65131af3782e2 Faulting application path: C:\Program Files\WindowsApps\MyApp\MyApp.exe Faulting module path: C:\Windows\System32\Windows.ApplicationModel.Store.dll Report Id: 4a027389-345c-4f16-a605-276f2774c768 Faulting package full name: MyApp Faulting package-relative application ID: App

这种异常好像用托管代码是抓不到的,怎么办?

和其他工程师讨论过后,看来没有其他办法了。 此方法应在后台 运行 以便您无法在 UI 线程中捕获它。它基本上绕过了开发人员处理异常的任何机会。

我们的建议是调用该方法前需要检查网络,调用该方法时提示用户保持网络连接。您可以尝试在您的应用中订阅 the NetworkInformation.NetworkStatusChanged Event