Search code examples
c#windows-store-appswinrt-xaml

Deadlock in IUriToStreamResolver when making synchronous AJAX requests


I am trying to inject my application resources into a Windows 8.1 WinRT WebView using an IUriToStreamResolver, as described here: https://blogs.msdn.microsoft.com/wsdevsol/2014/06/20/a-primer-on-webview-navigatetolocalstreamuri/

However, every time I'm starting a synchronous XHR from my JavaScript the app apparently runs into a deadlock. The same code works fine in a Windows 10 UWP app.

Unfortunately I have to use synchronous requests. Is it possible to avoid the deadlock?

index.html:

<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
    <span id="progress">Loading...</span>

    <script type="text/javascript">
        // Set timeout to show the page before causing the deadlock
        setTimeout(function () {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/index.html', /* async */ false);
            xhr.onload = function () {
                /* this will never be called... */
                document.getElementById('progress').textContent = 'Success';
            };
            xhr.onerror = function () {
                /* this will never be called... */
                document.getElementById('progress').textContent = 'Error';
            };
            xhr.send();
        }, 1000);
    </script>
</body>
</html>

MainPage.xaml.cs:

private void Page_Loaded(object sender, RoutedEventArgs e) {
    // webview is <WebView x:Name="webview" />
    var uri = webview.BuildLocalStreamUri("Test", "index.html");
    webview.NavigateToLocalStreamUri(uri, new StreamUriWinRTResolver());
}

// I took this from the MSDN example
public sealed class StreamUriWinRTResolver : IUriToStreamResolver {
    public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri) {
        if (uri == null)
            throw new Exception();
        string path = uri.AbsolutePath;
        return GetContent(path).AsAsyncOperation();
    }

    private async Task<IInputStream> GetContent(string path) {
        try {
            // Load directly from appx in this example
            Uri localUri = new Uri("ms-appx://" + path);
            // This line is causing the app to hang:
            StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
            IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
            return stream;
        } catch (Exception) { throw new Exception("Invalid path"); }
    }
}

Solution

  • In my case the solution was to write the IUriToStreamResolver in C++.

    I used this example class provided by Microsoft, which doesn't cause problems with synchronous XHRs: https://code.msdn.microsoft.com/HTML-WebView-control-sample-56e773fa/sourcecode?fileId=99260&pathId=523934392

    I can't really explain why this makes a difference, but the fact that UWP behaves differently makes me think that this could be a bug in the framework.