我正在开发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&amp; Firefox没有这样的限制,我可以异步发送消息给stdout。
更新#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();
}
}
答案 0 :(得分:0)
您需要致电args.GetDeferral()
,以避免在第一次暂停通话(await
)时断开连接。然后,一旦完成工作,您需要在延期上致电Complete()
。