none
"A COM call to an ASTA was blocked" exception from Windows Runtime Component only in Mobile

    Question

  • Hello all. 

    I have the following setup in my UWP. 

    There is a WebView which displays a site. I have some control over the javascript in the site and I am able to inject a WebAllowedObject from a WindowsRuntimeComponent into the page and use it from there. 

    In the WindowsRuntimeComponent I expose an event. My application code subscribes to this event and javascript code triggers it.
    The code for this is below.

    Windows Runtime Component

    public delegate void NotifyAppHandler( string str );
    
    [AllowForWeb]
    public sealed class WebViewInjectionObject
    {
        public event NotifyAppHandler OnNotifyApp;
    
        public void NotifyApp( string str )
        {
            OnNotifyApp?.Invoke( str );
        }
    }

    JavaScript

    nativeObject.notifyApp(astring)

    Application code

    webViewInjectionObject = new WebViewInjectionObject();
     webViewInjectionObject.OnNotifyApp += WebViewInjectionObject_OnNotifyApp;  
    
    
    private void WebView_NavigationStarting( WebView sender, WebViewNavigationStartingEventArgs args ) {
            sender.AddWebAllowedObject( "nativeObject", webViewInjectionObject );
        }
    
    

    After the event is triggered in JavaScript I need to call WebView.InvokeScriptAsync(...) and there the problem occurs.

    Application code inside the event handler WebViewInjectionObject_OnNotifyApp

     await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
          {
               await WebView.InvokeScriptAsync( successFunctionName, functionArguments );
          } );

    When I run this code I get the following exception 

    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. A COM call (IID: {638BB2DB-451D-4661-B099-414F34FFB9F1}, method index: 6) to an ASTA (thread 3036) was blocked because the call chain originated in or passed through another ASTA (thread 5964). This call pattern is deadlock-prone and disallowed by apartment call control.

    But this only happens on Mobile and not on Desktop. 

    I have tried a lot of other solutions including

    --creating async methods in the WRC and using them from javascript

    --using the synchronization context to Marshal the call.  

    but all of them go back to this exception.

    I am thinking that the problem is not how to make two threads communicate with each other, but that the two threads I am trying to make communicate are not allowed to do so. 
    Maybe in Windows 10 Mobile the Application and the WebView have two distinct Application Single Thread Appartments and  in Windows 10 they don't, I don't know.

    What can I do to solve this problem??

    Friday, April 27, 2018 4:54 PM

All replies

  • @Corcus,

    Can you provide your Phone OS version and your worked PC OS version for us?

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 30, 2018 6:12 AM
    Moderator
  • Hi Barry and thanks for looking into this.

    For the Desktop that the issue does not appear the version of the OS is 1709.

    For the phone it doesn't work for version 1709 and also for a previous one (as soon as I can access the device again I will update my reply with the accurate number of the second version)

    Monday, April 30, 2018 8:27 PM
  • @Corcus,

    Tried to reproduce this on a 1709 Phone, OS build:10.0.15254.313 but failed to reproduce this exception. Which line reported this exception and what are details about your js code? Would you mind to share a simple sample which can reproduce this issue as a MVCE?

    I tried to add a windows runtime component, add Web_OnNavigationStarting and add Web_OnScriptNotify event. I haven't wrote code under WinRTObject_NotifyAppEvent.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 1, 2018 6:22 AM
    Moderator
  • @Barry

    The line that throws the exception is 

    await WebView.InvokeScriptAsync( successFunctionName, functionArguments );

    It is when I am trying to access the WebView from inside the event handler WebViewInjectionObject_OnNotifyApp.

    I will try to create a repro project and post it here.

    Thanks again for looking into this.

    Also the Windows 10 mobile versions I have tried this on are 1709 and 1607.

    Wednesday, May 2, 2018 8:00 AM
  • @ Barry

    Hello again.

    Here is a repro project as you requested.

    This works fine on Desktop and throws the same exception on mobile

    https://1drv.ms/u/s!Ah9k2k7dg6rCkhXfay8-YHHBEzne

    Friday, May 4, 2018 9:47 AM
  • @Corcus,

    Sorry it's a bit late. Can you share the demo again?

    Regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 2:26 AM
    Moderator