来自 Edge 浏览器的每个请求都需要发送相应的响应。如果不是,则伴随的 UWP(以及关联的 Win32 应用程序,如果有)退出并引用“SystemPolicy”作为原因。为了说明问题,我可以参考SecureInput示例。
/// <summary>
/// Receives message from Extension (via Edge)
/// </summary>
private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
AppServiceDeferral messageDeferral = args.GetDeferral();
try
{
if (this.desktopBridgeAppLaunched)
{
this.currentConnectionIndex = Int32.Parse(sender.AppServiceName);
this.desktopBridgeConnection = desktopBridgeConnections[this.currentConnectionIndex];
// Send message to the desktopBridge component and wait for response
AppServiceResponse desktopBridgeResponse = await this.desktopBridgeConnection.SendMessageAsync(args.Request.Message);
await args.Request.SendResponseAsync(desktopBridgeResponse.Message);
}
else
{
throw new Exception("Failed to launch desktopBridge App!");
}
}
finally
{
messageDeferral.Complete();
}
}
通过注释掉调用“SendResponseAsync(...)”的行,UWP 和 Win32 应用程序(PasswordInputProtection.exe) 将退出,因为使用 SystemPolicy 原因调用了“OnAppServicesCanceled(...)”处理程序。
我知道对于这个特定的示例,不发回响应是没有意义的。但是我有不需要向边缘扩展发送回响应的情况。相反,我打算使用 Win32 应用程序中的 SendMessageAsync(...) 通过 UWP 应用程序与扩展程序通信。
此外,我注意到我在网上找到的本机消息传递示例至少会从 UWP 将无用的 ACK 消息发送回扩展程序。那么,是设计需要发送响应还是我在这里遗漏了什么?