【发布时间】:2013-03-13 21:37:41
【问题描述】:
我有带有代码的便携式类库(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”,它将毫无例外地工作。
在第二次启动时它始终有效。
有什么建议吗?
【问题讨论】:
-
只是好奇,你为什么在 win 8 下使用 .net 4.0 - 我想,win 8 默认有 4.5,不是吗?
-
@Isantipov 可移植类库意味着它是一个可以从各种版本的框架中使用的库。
-
@svick 完全正确
-
尝试捕获异常并查看详细信息...
-
@PeterRitchie 正如您在上面的代码中看到的那样,我在该方法中使用 try/catch
标签: c# portable-class-library async-await