问题
I am developing an extension for Edge where I need to send more than 1MB of data to the extension from UWP.
I have chunked the data to be less than 1MB and tried to send reply multiple times something like below
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();
}
The app service is cancelled when sending the second response in the above loop. It seems like a response can be sent only if there is a corresponding request. Can someone confirm this?
Are there any alternative mechanisms in UWP to send the data asynchronously without a request?
Another option I can think of is to have the chunking logic moved to the background and it doesn't feel right.
FWIW, Chrome & Firefox doesn't have such limitation and I am able to send messages to stdout asynchronously.
Update#1: Peter, I am afraid I get the same error "A method was called at an unexpected time" for the second sendReply. I tried the following and hope this is what you meant.
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();
}
}
回答1:
You need to call args.GetDeferral()
in order to avoid having your connection torn down at the first suspension call (await
). Then you need to call Complete()
on the deferral once your work is done.
来源:https://stackoverflow.com/questions/47980665/edge-extension-native-messaging-dealing-with-uwp-message-size-limit