我有便携式类库(net40 + SL4 + win8的+ wp71)与代码:
异常使用的await HttpClient.GetAsync便携式类库
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和微软.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()
编辑:如果我注释行“数据=等待httpClient.GetStringAsync”,将工作无异常。
在第二次启动时它始终工作。
有什么建议吗?
只是好奇,你为什么使用.NET 4.0下的胜8 - 我认为,赢8默认是4.5,不是吗? – Isantipov 2013-03-13 22:34:33
@Isantipov可移植类库意味着它是一个可以从各种框架版本使用的库。 – svick 2013-03-13 23:03:23
@svick完全正确 – KondraT 2013-03-14 13:32:31