Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception using await HttpClient.GetAsync in Portable Class Library

I have Portable Class Library(net40+sl4+win8+wp71) with code:

    public static async Task<Dictionary<string, FeedItem>> GetFeeds()  
    {  
        try
        {
            string data;
            using (HttpMessageHandler handler = new HttpClientHandler())
            {
                using (HttpClient httpClient = new HttpClient(handler))
                {
                    data = await httpClient.GetStringAsync(FeedsUrl + Guid.NewGuid()).ConfigureAwait(false);
                }
            }

            if (data != null)
            {
                //...
            }
            return null;
        }
        catch
        {
            return null;
        }
    }

Microsoft.Bcl, Microsoft.Bcl.Async and Microsoft.Net.Http are referenced.

I use this library in Windows 8 Metro app. Then I debugg app through VS2012, it is ok. But when I create App Package and install it, the app crashes on first launch everytime with error:

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.COMException
Stack:
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__1(System.Object)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

Edit: If I comment line "data = await httpClient.GetStringAsync" it will work without exception.

On second launch it always works.

Any suggestion?

like image 990
KondraT Avatar asked Jul 02 '26 02:07

KondraT


1 Answers

These packages have framework specific implementations. You may be hitting a problem because you are not referencing the framework specific assembly from your Win8 app.

Can you try to reference the Nuget packages from your application and see if it resolves the problem? Nuget doesn't install packages up-stack, so when you create a class library that consumes another Nuget package you need to manually add that Nuget package to projects that reference the class library.

This happens transparently when the class library is packaged as a nupkg through package dependencies, but when you are using a project or binary reference you need to do it yourself.

like image 88
TheESJ Avatar answered Jul 04 '26 15:07

TheESJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!