Share target error when trying to read storageItem file in win 8.1

1

I'm trying to implement share target app contract in win 8.1. Sample share target app code works, but when I try to read storageItem file using following code:

storageItems.getAt(i).openReadAsync().then(function(stream) {

});

I get an error:

0x8000001f - JavaScript runtime error: A COM call to an ASTA was blocked because the call chain originated in or passed through another ASTA. This call pattern is deadlock-prone and disallowed by apartment call control.

Is it another WinJs/Win8.1 preview bug or I'm doing something wrong?

com
winjs
windows-8.1
asked on Stack Overflow Jul 22, 2013 by Tadeusz Wójcik • edited Jul 22, 2013 by Tadeusz Wójcik

1 Answer

9

I ran into this same problem. It's unfortunate that in all of the examples and tutorials and sample code that MSFT put out about share contract targets, not a single one of them actually reads the shared file.

I don't claim to understand specifically what's going on under the covers, but it involves both the UI thread of the share target (your app) and the UI thread of the share source being on the call stack at the same time during the OpenReadAsync call, which is what causes the freakout.

The solution is to move your OpenReadAsync call off of your UI thread. Sorry for not knowing the JS way of doing this, but the way I fixed my version of this problem in C# was:

// BAD - This produces: "A COM call to an ASTA was blocked because the call  
// chain originated in or passed through another ASTA. This call pattern 
// is deadlock-prone and disallowed by apartment call control."
//
// IRandomAccessStreamWithContentType stream = await fileReceived.OpenReadAsync(); 

// GOOD - This moves the OpenReadAsync off of the UI thread and works fine...
//
IRandomAccessStreamWithContentType stream = await Task.Run(async () =>
{
    return await fileReceived.OpenReadAsync(); 
});
answered on Stack Overflow Sep 5, 2013 by BobDickinson

User contributions licensed under CC BY-SA 3.0