我有一个可移植类库(net40+sl4+win8+wp71),其代码如下:
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和Microsoft.Net.Http。
我在Windows 8 Metro应用程序中使用这个库。然后我通过VS2012调试应用程序,一切正常。但是当我创建应用程序包并安装它时,每次第一次启动应用程序都会崩溃,并显示错误:
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()
编辑:如果我注释掉“data = await httpClient.GetStringAsync”这一行,它就能正常运行而不会出现异常。
在第二次启动时它总是可以正常工作的。
有什么建议吗?