Exception using await HttpClient.GetAsync in Portable Class Library

馋奶兔 提交于 2019-12-04 12:47:11

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.

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.

KondraT

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!