3

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?

5
  • just curious, why are you using .net 4.0 under win 8 - I supposed, win 8 has 4.5 by default, doesn't it? Commented Mar 13, 2013 at 22:34
  • 1
    @Isantipov Portable class library means it's a library that can be used from various versions of the framework. Commented Mar 13, 2013 at 23:03
  • @svick is completely right Commented Mar 14, 2013 at 13:32
  • try catching the exception and looking at the details... Commented Mar 14, 2013 at 19:01
  • @PeterRitchie as you can see in code above I use try/catch in that method Commented Mar 14, 2013 at 19:06

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Just guessing, but it looks like you are getting an exception which you are not handling, probably in an async void method. When the await call resumes it is throwing the exception, but because it just resumed from await the call stack doesn't reflect where in your code the failure is occurring.

I would recommend wrapping the body of any async void methods you have in a try/catch block that will report the exception and where in your code it is occurring. This should help figure out what is wrong. Once you have the exception, looking at it's HResult property may also help with this.

1 Comment

I forgot to mention that it happens on line "data = await httpClient.GetStringAsync". So if you comment it then it will work fine.
1

I've figured it out. I use MVVM Light, so I have ViewModelLocator with code:

public ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    SimpleIoc.Default.Register<MainViewModel>(true);
}

So when I removed immediate creation of the model it stopped crashing:

SimpleIoc.Default.Register<MainViewModel>(/*true*/);

I have no idea why but I think it's because MVVM Light has some problems with Microsoft.Bcl, Microsoft.Bcl.Async or Microsoft.Net.Http.

Hope this will help someone, e.g. this question

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.