“操作无效"; HttpClient的Xamarin.iOS项目出现错误 [英] "Operation is not valid" error at Xamarin.iOS project with HttpClient

查看:128
本文介绍了“操作无效"; HttpClient的Xamarin.iOS项目出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建HttpClient并在按钮单击处理程序上调用GetStringAsync方法:

I create HttpClient and call GetStringAsync method right at the button click handler:

var client = new HttpClient();
var response = await client.GetStringAsync("http://google.com");
Debug.WriteLine("Response received: {0}", response);

并且每次出现以下错误:

And every time I get the following error:

System.InvalidOperationException: Operation is not valid due to the current state of the object

2014-05-10 01:26:36.657 HelloWorld3[490:60b] Unhandled managed exception: Operation is not valid due to the current state of the object (System.InvalidOperationException)
  at System.Lightup.Call[HttpWebRequest,Int64] (System.Delegate& storage, System.Net.HttpWebRequest instance, System.String methodName, Int64 parameter) [0x00000] in <filename unknown>:0 
  at System.Lightup.Set[HttpWebRequest,Int64] (System.Delegate& storage, System.Net.HttpWebRequest instance, System.String propertyName, Int64 value) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequestLightup.SetContentLength (System.Net.HttpWebRequest instance, Int64 value) [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpWebRequest.set_ContentLength (Int64 value) [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClientHandler.StartRequest (System.Object obj) [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispat
chInfo.Throw () [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
  at System.Runtime.CompilerServices.TaskAwaiter`1[System.String].GetResult () [0x00000] in <filename unknown>:0 
  at HelloWorld3.MyViewController+<<ViewDidLoad>b__0>d__1.MoveNext () [0x00029] in c:\Sources\Local\HelloWorld3\HelloWorld3\MyViewController.cs:41 
Debugging session ended.
The program 'Mono' has exited with code 0 (0x0).

我没有使用可移植的库,nuget等,只是从模板创建的普通项目. 如何解决这个问题?

I'm not using portable libs, nugets, etc. Just plain project created from a template. How to solve this issue?

测试项目,此处有复制的问题: https://dl.dropboxusercontent.com/u/19503836/HelloWorld3.zip

test project with reproduced issue here: https://dl.dropboxusercontent.com/u/19503836/HelloWorld3.zip

推荐答案

通常,当您使用Windows中的 PCL 程序集构建应用程序时,会发生这种情况. /p>

In general this happens when you're building your application using PCL assemblies and from Windows.

我没有使用可移植库,

I'm not using portable libs,

好的,您仍然有可能陷入同样的​​境地.在Mac上复制(和编译)的System.Http.Net.dll版本错误(MS不是XI).例如

Ok, still you're likely falling into the same situation. You have the wrong (MS not XI) version of System.Http.Net.dll being copied (and compiled) on the Mac. E.g.

在System.Lightup ....

at System.Lightup....

不是Xamarin.iOS随附的程序集的一部分.在堆栈跟踪中包含此符号表示使用了错误的程序集.

Is not part of the assembly being shipped with Xamarin.iOS. Having this in your stack trace is an indication that the wrong assembly is being used.

这是一个已解决的已知问题(至少对于PCL部分而言).同时,常规解决方案正在使用重定向文件(请参见 James Blog )或从Mac构建.

This is a known issue that is being fixed (at least for the PCL part). In the mean time the general workaround is using a redirect file (see James blog) or build from the Mac.

在您的特定情况下,您可能只需要确保引用了正确的程序集即可.

In your specific case you might just need to ensure the right assembly is being referenced.

这篇关于“操作无效"; HttpClient的Xamarin.iOS项目出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆