问题
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?
回答1:
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.
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/15396981/exception-using-await-httpclient-getasync-in-portable-class-library