1

我正在为 Edge 开发一个扩展,我需要从 UWP 向扩展发送超过 1MB 的数据。

我已将数据分块小于 1MB,并尝试多次发送回复,如下所示

private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) {
  AppServiceDeferral messageDeferral = args.GetDeferral();

  // This is to be replaced with enumerator
   for (int i = 0; i < number_of_chunks; i++) {
      // construct reply identifying the chunk number and termination
       await args.Request.SendResponseAsync(reply);
   }
   messageDeferral.Complete();

}

在上述循环中发送第二个响应时,应用服务被取消。似乎只有在有相应请求的情况下才能发送响应。有人可以证实这一点吗?

UWP 中是否有任何替代机制可以在没有请求的情况下异步发送数据?

我能想到的另一个选择是将分块逻辑移到后台,但感觉不对。

FWIW、Chrome 和 Firefox 没有这样的限制,我可以将消息异步发送到标准输出。

更新#1:彼得,我担心第二个 sendReply 收到相同的错误“在意外时间调用了一个方法”。我尝试了以下方法,希望这就是您的意思。

private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) {

  // This is to be replaced with enumerator
   for (int i = 0; i < number_of_chunks; i++) {
      // construct reply identifying the chunk number and termination
      AppServiceDeferral messageDeferral = args.GetDeferral();
      await args.Request.SendResponseAsync(reply);
      messageDeferral.Complete();
   }
}
4

1 回答 1

0

您需要致电args.GetDeferral()以避免在第一次暂停呼叫 ( await) 时断开连接。然后你需要Complete()在你的工作完成后调用延期。

于 2017-12-27T07:14:06.993 回答