简体   繁体   English

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

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

I have Portable Class Library(net40+sl4+win8+wp71) with code: 我有可移植类库(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 and Microsoft.Net.Http are referenced. 引用了Microsoft.Bcl,Microsoft.Bcl.Async和Microsoft.Net.Http。

I use this library in Windows 8 Metro app. 我在Windows 8 Metro应用程序中使用此库。 Then I debugg app through VS2012, it is ok. 然后我通过VS2012调试app,没关系。 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. 编辑:如果我评论行“data = await httpClient.GetStringAsync”它将毫无例外地工作。

On second launch it always works. 在第二次发布时,它始终有效。

Any suggestion? 有什么建议吗?

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. 您可能遇到问题,因为您没有从Win8应用程序引用特定于框架的程序集。

Can you try to reference the Nuget packages from your application and see if it resolves the problem? 您是否可以尝试从应用程序中引用Nuget包,看看它是否可以解决问题? 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. Nuget不会向上堆栈安装包,因此当您创建一个使用另一个Nuget包的类库时,您需要手动将该Nuget包添加到引用该类库的项目中。

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

Just guessing, but it looks like you are getting an exception which you are not handling, probably in an async void method. 只是猜测,但看起来你得到一个你没有处理的异常,可能是在async void方法中。 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. 当await调用恢复时,它会抛出异常,但由于它刚从await恢复,因此调用堆栈不会反映代码中发生故障的位置。

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. 我建议在try / catch块中包装任何异步void方法的主体,该方法将报告异常以及代码中的位置。 This should help figure out what is wrong. 这应该有助于找出问题所在。 Once you have the exception, looking at it's HResult property may also help with this. 一旦你有异常,查看它的HResult属性也可能有助于此。

I've figured it out. 我已经明白了。 I use MVVM Light, so I have ViewModelLocator with code: 我使用MVVM Light,所以我有ViewModelLocator代码:

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. 我不知道为什么,但我认为这是因为MVVM Light在Microsoft.Bcl,Microsoft.Bcl.Async或Microsoft.Net.Http方面存在一些问题。

Hope this will help someone, eg this question 希望这会帮助某人,例如这个问题

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

相关问题 HttpClient.GetAsync(...) 在使用 await/async 时永远不会返回 - HttpClient.GetAsync(…) never returns when using await/async HttpClient.GetAsync,无效的URI异常 - HttpClient.GetAsync, Invalid URI exception HttpClient.getAsync()内存不足异常 - HttpClient.getAsync() Out of Memory Exception Xamarin Android httpClient.getAsync导致异常 - Xamarin Android httpClient.getAsync causes Exception HttpClient 在 GetAsync 上使用 await 时不会抛出异常 - HttpClient not throwing exception when using await on GetAsync HttpClient.GetAsync在一个类中起作用,但在另一个类中不起作用 - HttpClient.GetAsync works in one class but not in the other 使用 httpClient.GetAsync 时添加标头 - Adding headers when using httpClient.GetAsync 异步等待 Task.Run 与 HttpClient.GetAsync - Async-await Task.Run vs HttpClient.GetAsync 在服务结构服务中使用HttpClient.GetAsync会抛出“无法连接到远程服务”异常 - using HttpClient.GetAsync inside a service fabric service throws “unable to connect to remote service” exception httpClient.GetAsync() 超时异常,如果应用程序空闲很长时间 - httpClient.GetAsync() timeout exception, if application idle a long time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM