Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Operation is not valid" error at Xamarin.iOS project with HttpClient

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

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

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

like image 326
Alexey Strakh Avatar asked May 10 '14 05:05

Alexey Strakh


2 Answers

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

I'm not using portable libs,

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.

at System.Lightup....

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.

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.

like image 118
poupou Avatar answered Oct 05 '22 20:10

poupou


i had the same problem! resolve with FlorianZimmermann solution.

on this link: 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. right-click on References directory for ios or android solution
  2. select edit references
  3. in packages tab , check System.Net.Http
  4. build!

Hope it's help.

like image 34
VashTheSoulkeeper Avatar answered Oct 05 '22 21:10

VashTheSoulkeeper