菜鸟教程小白 发表于 2022-12-12 22:40:41

ios - C# - Xamarin - HttpClient - 由于对象的当前状态,操作无效 - iOS


                                            <p><p>我正在开发一个发出 HTTP 请求的跨平台库。它在 Android 上运行良好,但是当我尝试在 iOS 上使用它时出现异常,我不知道如何修复它。</p>

<p>这是我的代码:</p>

<pre><code>// method from cross platform library

Task.Factory.StartNew(delegate
{
    try
    {
      var client = new HttpClient();
      // some other setup stuff
      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.post, &#34;http://myurl.com...&#34;);

      var task = client.SendAsync(request);
      task.Wait(); // Exception thrown on this line

      var response = task.Result;

      var responseString = response.Content.ReadAsStringAsync().Result;
    }
    catch (Exception e)
    {
    }
}
</code></pre>

<p>在 <code>task.Wait();</code> 我得到一个 <code>System.AggregateException</code> 内部异常 <code>System.InvalidOperationException</code> 说 <code>由于对象的当前状态,操作无效。</code></p>

<p>试图找到一些解决方案,我发现问题可能是通过在 UI 线程上调用它引起的。但这就是将这一切包装在 <code>Task.Factory.StartNew</code> 中的全部意义所在。</p>

<p>我已经尝试了所有我知道的方法,但仍未解决问题。任何帮助将不胜感激。</p>

<p><strong>编辑:</strong></p>

<p>我决定在 iPhone 模拟器上尝试我的解决方案。这是一个运行 iOS 10 的 iPhone 6 模拟器。我的物理设备是一样的。它可以在模拟器上运行,但由于某种原因不能在物理设备上运行……很奇怪。</p>

<p><strong>编辑 2:</strong></p>

<p>感谢@YuriS 找到解决方案。</p>

<p>发件人:<a href="https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec" rel="noreferrer noopener nofollow">https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec</a> </p>

<p>你可以做的是:
1)转到ios项目的引用资料
2) 编辑引用文献
3)检查'System.Net.Http'</p>

<p>android 的行为是一样的。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这里描述的问题可能很少:
<a href="https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec" rel="noreferrer noopener nofollow">https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec</a> </p>

<p> <a href="https://bugzilla.xamarin.com/show_bug.cgi?id=17936" rel="noreferrer noopener nofollow">https://bugzilla.xamarin.com/show_bug.cgi?id=17936</a> </p>

<p> <a href="https://stackoverflow.com/questions/23577465/operation-is-not-valid-error-at-xamarin-ios-project-with-httpclient" rel="noreferrer noopener nofollow">&#34;Operation is not valid&#34; error at Xamarin.iOS project with HttpClient</a> </p>

<p> <a href="http://motzcod.es/post/78863496592/portable-class-libraries-httpclient-so-happy" rel="noreferrer noopener nofollow">http://motzcod.es/post/78863496592/portable-class-libraries-httpclient-so-happy</a> </p>

<p>似乎所有帖子都指向 System.Net.Http</p>

<p>不管问题如何,都有更好的方法来做到这一点。其中之一:</p>

<pre><code>public static async Task PostRequest()
{
    try
    {
      HttpClient client = new HttpClient();
      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, &#34;https://myuri&#34;);
      //request.Headers.Add(&#34;&#34;, &#34;&#34;);
      var response = await client.SendAsync(request);
      var responseString = await response.Content.ReadAsStringAsync();
    }
    catch (Exception ex)
    {

    }
}
</code></pre>

<p>如果你想等到函数完成你调用</p>

<pre><code>await PostRequest();
</code></pre>

<p>如果您不需要等待,则只需在调用中省略“等待”或使用</p>

<pre><code>PostRequest.ContinueWith((t)=&gt;
{
});
</code></pre>

<p>您还需要在函数中处理异常,因此可能只返回 Task 并不是最好的。我只是根据原始函数签名来回答</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - C# - Xamarin - HttpClient - 由于对象的当前状态,操作无效 - iOS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40244632/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40244632/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - C# - Xamarin - HttpClient - 由于对象的当前状态,操作无效 - iOS