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
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.
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
Hope it's help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With