我创建 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 https://dl.dropboxusercontent.com/u/19503836/HelloWorld3.zip
一般来说,当您使用构建应用程序时会发生这种情况PCL组件and来自 Windows。
我没有使用便携式库,
好吧,你仍然可能陷入同样的情况。您的版本错误(MS 不是 XI)System.Http.Net.dll
在 Mac 上复制(和编译)。例如。
在系统.Lightup....
不是 Xamarin.iOS 附带的程序集的一部分。如果堆栈跟踪中有此内容,则表明使用了错误的程序集。
这是一个正在修复的已知问题(至少对于 PCL 部分而言)。与此同时general解决方法是使用重定向文件(请参阅詹姆斯博客 http://motzcod.es/post/78863496592/portable-class-libraries-httpclient-so-happy)或从 Mac 构建。
在您的具体情况下,您可能只需要确保引用正确的程序集。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)