2

I am trying to add some caching to a WebView for a Windows Phone 8.1 WinRT application. I am using the new IUriToStreamResolver to get streams for certain urls and this works OK most of the time. For some urls I get the following exception though:

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at System.Threading.Tasks.TaskToAsyncOperationAdapter1.OnCompleted(AsyncOperationCompletedHandler1 userCompletionHandler, AsyncStatus asyncStatus) at System.Threading.Tasks.TaskToAsyncInfoAdapter4.OnCompletedInvoker(AsyncStatus status) at System.Threading.Tasks.TaskToAsyncInfoAdapter4.TaskCompleted() --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.ExceptionDispatchHelper.b__1(Object edi) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

My UriToStreamResolver uses System.Net.HttpClient to get data from the web and convert the relative url to an absolute URL.

using System;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage.Streams;
using Windows.Web;

namespace OfflineBrowser
{
    public sealed class StreamUriWinRTResolver : IUriToStreamResolver
    {
        readonly System.Net.Http.HttpClient _httpClient = new System.Net.Http.HttpClient();
        private readonly string _baseUrl;

    public StreamUriWinRTResolver(string baseUrl)
    {
        _baseUrl = baseUrl;
    }

    public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
    {
        try
        {
            if (uri == null)
            {
                throw new Exception();
            }
            string path = uri.AbsolutePath;
            return GetContent(path).AsAsyncOperation();

        }
        catch (Exception)
        {
            return null;
        }
    }

    private async Task<IInputStream> GetContent(string uriPath)
    {
        try
        {
            var url2 = _baseUrl + uriPath;
            var bytes = await _httpClient.GetByteArrayAsync(new Uri(url2));
            IInputStream inputStream = await GetStreamForByteArray(bytes);
            return inputStream;

        }
        catch (Exception)
        {
            //catch not working.. 
            return null;
        }
    }

    private static async Task<IInputStream> GetStreamForByteArray(byte[] content2)
    {
        var ras = new InMemoryRandomAccessStream();
        var datawriter = new DataWriter(ras);
        datawriter.WriteBytes(content2);
        await datawriter.StoreAsync();
        await datawriter.FlushAsync();
        return ras.GetInputStreamAt(0);
    }
}

}

I call it like this:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        Uri MyUrl = WebView.BuildLocalStreamUri("MyContent", "/tvgids");
        StreamUriWinRTResolver MyResolver = new StreamUriWinRTResolver("http://www.nu.nl");
        WebView.NavigateToLocalStreamUri(MyUrl, MyResolver);
    }

I tried catching the exception but that doesn't work either. What could be wrong here? I encounter the error both in simulator and on a real device. Thanks for reading.

Gluip
  • 2,917
  • 4
  • 37
  • 46

1 Answers1

0

Check if uri in UriToStreamAsync is valid. For some reason it gets corrupted for example "http://www.test.com/<a href" as borwser is trying to resolve unknow url from html body and then it throws fatal exception.

awattar
  • 446
  • 5
  • 19