繁体   English   中英

在可移植类库中使用等待HttpClient.GetAsync的异常

[英]Exception using await HttpClient.GetAsync in Portable Class Library

我有可移植类库(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调试app,没关系。 但是当我创建应用程序包并安装它时,应用程序每次首次启动时都会崩溃并出现错误:

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”它将毫无例外地工作。

在第二次发布时,它始终有效。

有什么建议吗?

这些包具有特定于框架的实现。 您可能遇到问题,因为您没有从Win8应用程序引用特定于框架的程序集。

您是否可以尝试从应用程序中引用Nuget包,看看它是否可以解决问题? Nuget不会向上堆栈安装包,因此当您创建一个使用另一个Nuget包的类库时,您需要手动将该Nuget包添加到引用该类库的项目中。

当类库通过包依赖性打包为nupkg时,这会透明地发生,但是当您使用项目或二进制引用时,您需要自己完成。

只是猜测,但看起来你得到一个你没有处理的异常,可能是在async void方法中。 当await调用恢复时,它会抛出异常,但由于它刚从await恢复,因此调用堆栈不会反映代码中发生故障的位置。

我建议在try / catch块中包装任何异步void方法的主体,该方法将报告异常以及代码中的位置。 这应该有助于找出问题所在。 一旦你有异常,查看它的HResult属性也可能有助于此。

我已经明白了。 我使用MVVM Light,所以我有ViewModelLocator代码:

public ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    SimpleIoc.Default.Register<MainViewModel>(true);
}

因此,当我立即删除模型时,它停止了崩溃:

SimpleIoc.Default.Register<MainViewModel>(/*true*/);

我不知道为什么,但我认为这是因为MVVM Light在Microsoft.Bcl,Microsoft.Bcl.Async或Microsoft.Net.Http方面存在一些问题。

希望这会帮助某人,例如这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM