使用HttpClient在Xamarin.iOS项目中“操作无效”错误

时间:2014-05-10 05:32:01

标签: xamarin.ios xamarin httpclient

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

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

每次我收到以下错误:

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).

我没有使用便携式库,nugets等。只是从模板创建的普通项目。 如何解决这个问题?

测试项目,转载问题在这里: https://dl.dropboxusercontent.com/u/19503836/HelloWorld3.zip

4 个答案:

答案 0 :(得分:8)

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

  

我没有使用便携式库,

好的,你仍然可能陷入同样的​​境地。您在Mac上复制(和编译)了System.Http.Net.dll的错误(MS非XI)版本。 E.g。

在System.Lightup ....

不是Xamarin.iOS随附的组件的一部分。在堆栈跟踪中有这个表示正在使用错误的程序集。

这是一个已修复的已知问题(至少对于PCL部分而言)。与此同时,常规解决方法正在使用重定向文件(请参阅James blog)或从Mac构建。

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

答案 1 :(得分:2)

我有同样的问题! 用FlorianZimmermann解决方案解决。

在此链接上: https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec

  1. 右键单击ios或android解决方案的References目录
  2. 选择编辑参考
  3. 在包标签中,检查System.Net.Http
  4. 构建<!/ LI>

    希望有所帮助。

答案 2 :(得分:1)

另一种解决方法是将Web请求包含在注入PCL的服务中。 不太理想,但这就是我现在正在做的事情。

答案 3 :(得分:0)

在使用平台特定的“简单”处理程序和IoC魔术进入主要并发症之前,我建议您验证一些事情。毕竟,Xamarin和Mono的所有要点都是为了避免所有这些黑色的兽人入侵。

如果这不起作用,那么我会对它进行科学研究,并进行弹道......但首先从这里开始:

  1. 确保您的平台特定项目(MyProject.iOS或MyProject.Android)具有引用

  2. 下引用的System.Net.Http
  3. 确保您的PCL定位到您的平台特定项目目标支持的配置文件(截至撰写本文时,我使用“PCL 4.5 - Profile78”与iOS和Android项目)

  4. 祝你好运!