引发ObjectDisposedException但在UWP上不

引发ObjectDisposedException但在UWP上不

本文介绍了HttpClient.SendAsync在Xamarin.Forms Android上引发ObjectDisposedException但在UWP上不引发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Xamarin.Forms应用程序,并尝试使用一些JSON负载向我的Web服务器发布消息.

I'm writing a Xamarin.Forms application and I'm trying to do a Post to my webserver with some JSON payload.

这在UWP中工作正常,但是在Android上,我收到以下错误消息:

This works fine in UWP, but on Android I get the following error:

 System.ObjectDisposedException: Can not access a closed Stream.
  at System.IO.StreamHelpers.ValidateCopyToArgs (System.IO.Stream source, System.IO.Stream destination, System.Int32 bufferSize) [0x0003a] in <19853c43ab794d18ab1a33ecb65b3c4d>:0
  at System.IO.MemoryStream.CopyToAsync (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken) [0x00000] in <19853c43ab794d18ab1a33ecb65b3c4d>:0
  at Xamarin.Android.Net.AndroidClientHandler.WriteRequestContentToOutput (System.Net.Http.HttpRequestMessage request, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken) [0x0009a] in <2bd1b8f6e97f44bbb32ce356b78d4ce6>:0
  at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x002f4] in <2bd1b8f6e97f44bbb32ce356b78d4ce6>:0
  at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x002cf] in <2bd1b8f6e97f44bbb32ce356b78d4ce6>:0
  at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in <c85119bf7e3e421490ae6b5487992fff>:0
  at BB.App.Services.WebserviceHelper.PostToWebservice[T] (System.String url, System.String json) [0x0008d] in C:\_Sources\_BB\BB.App\BB.App\Services\WebserviceHelper.cs:74

在执行以下代码时会发生这种情况:

This happens when the following code is executed:

await httpClient.SendAsync(httpRequest);

我发现了此站点,在该站点中,建议不要将HttpClient的创建放在using语句中,我更改了此设置,但并没有解决我的问题.

I found this site where it recommends not putting the creation of the HttpClient in a using statement, I changed this but it didn't fix my problem.

我还发现了此错误,该错误应该在去年修复.老实说,这听起来像是最有可能的候选人,但我不知道确切该更新什么以在我的PC上获得此修复程序.我已经在解决方案中更新了所有xamarin.android.x引用,但到目前为止还没有运气.

I also found this bug that is supposed to be fixed last year. To be honest this sounds like the most likely candidate but I don't know exactly what to update to get this fix on my pc. I already updated all my xamarin.android.x references in my solution but no luck so far.

推荐答案

总而言之,解决我问题的方法是

So in conclusion what fixed my problem was to

  • 转到android项目的属性
  • 选择"android选项"标签,
  • 点击高级"
  • 将"httpclient实现"从"Android"设置为托管"

这篇关于HttpClient.SendAsync在Xamarin.Forms Android上引发ObjectDisposedException但在UWP上不引发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 17:29