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

  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.

Another workaround would be to wrap up your web request in a service that gets injected into the PCL. Not ideal but thats what Im doing for now.

Before getting into major complications with platform specific "simple" handlers and IoC magic, I suggest you validate a couple of things. After all the whole point of Xamarin and Mono are to avoid all these black santeria hacks to begin with.

If this doesn't work, I would then science the shit of it, and go ballistic... but start here first:

  1. Make sure your platform specific project (MyProject.iOS, or MyProject.Android) HAVE the System.Net.Http referenced under references

  2. Make sure your PCL is targeting the a Profile that is supported by your platform specific project targets (as of the time of this writing, I use "PCL 4.5 - Profile78" with iOS and Android projects)

Good luck!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top