单声道下的HttpClient抛出错误

时间:2017-04-25 14:19:18

标签: c# mono xamarin.forms

我正在尝试在Mono下使用HttpClient(在.NET Framework下运行正常),但我收到错误;

04-25 14:07:14.952 I/Choreographer( 3238): Skipped 144 frames!  The application may be doing too much work on its main thread.
[0:] An error occurred while sending the request
[0:]   at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.MoveNext () [0x0047a] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:383 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x000a9] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
  at MyWebClient+<MakeRequest>d__63.MoveNext () [0x0019b] in <4eeea402c92c428f818fd28bf1ed35bc>:0

我的代码;

var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, (int) TimeOut) };
foreach (var header in Headers)
{
    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}

// attempting to workaround Xamarin bug
// http://forums.xamarin.com/discussion/17770/length-required-error-in-http-post-since-latest-release
var requestcontent = new HttpRequestMessage(HttpMethod.Post, Url)
{
    Content = !string.IsNullOrEmpty(RequestStr)
        ? new StringContent(RequestStr, Encoding.UTF8, "application/json")
        : new ByteArrayContent(new byte[0])
};

using (HttpResponseMessage response = await httpClient.SendAsync(requestcontent))
{
    using (HttpContent content = response.Content)
    {
        ResponseStr = await content.ReadAsStringAsync();
    }
}

我试图围绕这个问题编写代码(详见HttpClientHandler.cs:383),但它仍然会引发错误,但在NET Standard&amp; NET46。

这里是Xamarin包的版本;

Xamarin.Android.Support.Animated.Vector.Drawable.23.3.0
Xamarin.Android.Support.Design.23.3.0
Xamarin.Android.Support.v4.23.3.0
Xamarin.Android.Support.v7.AppCompat.23.3.0
Xamarin.Android.Support.v7.CardView.23.3.0
Xamarin.Android.Support.v7.MediaRouter.23.3.0
Xamarin.Android.Support.v7.RecyclerView.23.3.0
Xamarin.Android.Support.Vector.Drawable.23.3.0
Xamarin.Forms.2.3.4.231

1 个答案:

答案 0 :(得分:0)

  

应用程序可能在其主线程上做了太多工作。

当您尝试在主UI线程上执行HTTP请求时,通常会发生这种情况。