Bug 17936 - HttpClient fails in iOS ("Operation is not valid due to the current state of the object")
Summary: HttpClient fails in iOS ("Operation is not valid due to the current state of ...
Status: RESOLVED DUPLICATE of bug 20589
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: master
Hardware: PC Windows
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-02-21 12:50 UTC by Marcel Bradea
Modified: 2014-06-13 17:43 UTC (History)
7 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Marcel Bradea 2014-02-21 12:50:37 UTC
When executing a basic HttpClient call within a PCL project the call to "httpClient.GetAsync(url)" fails with an InvalidOperationException. This ONLY fails on iOS and the same code runs fine in the Android project.

The code being executed resides in a Project.Core PCL library referenced by Project.iOS and Project.Android respectively. The PCL library targets the following platforms: .NET Framework 4.5 and higher, Windows Phone 8, Xamarin.Android, Xamarin.iOS

using (HttpClient httpClient = new HttpClient()) {
	try {
		string url = string.Format("....URL....");
		Task<HttpResponseMessage> responseOp = httpClient.GetAsync(url);


Stack trace:
"Operation is not valid due to the current state of the object"
at System.Lightup.Call[HttpWebRequest,Int64] (System.Delegate& storage, System.Net.HttpWebRequest instance, System.String methodName, Int64 parameter) [0x00000]
	in <filename unknown>:0 \n
at System.Lightup.Set[HttpWebRequest,Int64] (System.Delegate& storage, System.Net.HttpWebRequest instance, System.String propertyName, Int64 value) [0x00000]
	in <filename unknown>:0 \n
at System.Net.HttpWebRequestLightup.SetContentLength (System.Net.HttpWebRequest instance, Int64 value) [0x00000]
	in <filename unknown>:0 \n
at System.Net.Http.HttpWebRequest.set_ContentLength (Int64 value) [0x00000]
	in <filename unknown>:0 \n
at System.Net.Http.HttpClientHandler.StartRequest (System.Object obj) [0x00000]
	in <filename unknown>:0 \n
--- End of stack trace from previous location where exception was thrown ---\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b]
	in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 \n
at System.Runtime.CompilerServices.TaskAwaiter`1[System.Net.Http.HttpResponseMessage].GetResult () [0x00000]
	in <filename unknown>:0 \n
at PanAMMobile.Core.Services.AQHIService+<GetLatestObservation>d__0.MoveNext () [0x000be]
	in c:\\Users\\mbradea\\Documents\\Projects\\PanAm-Git\\POC\\Xamarin\\PanAM-Mobile.Core\\Services\\AQHIService.cs:24

- Windows 7
- Visual Studio 2013 Update 1
- Xamarin.iOS 1.10.39

Tested the same code using the HttpWebRequest instead of HttpClient and the code worked perfectly (responseString is parsed)

var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.BeginGetResponse(callback => {
	using (var response = (HttpWebResponse)request.EndGetResponse(callback)) {
		Stream stream = response.GetResponseStream();
		string responseString = new StreamReader(stream).ReadToEnd();

}, state: null);
Comment 1 Sadik Ali 2014-02-24 11:19:34 UTC
I have checked this issue with following steps:

1. Created iOS project.
2. Created PCL project.
3. Added bug description code (HttpClient ) into pcl project.
4. Call that function in iOS project.
5. I applied check point and noticed that code is working and no exception generated.

Refer screen shot: http://screencast.com/t/VugFDgd4eyqs

Checked With:

Window 7
Visual Studio 2013, 2012
Xamarin.iOS 1.10.39

Let me know if I am missing anything
Comment 2 Rolf Bjarne Kvinge [MSFT] 2014-02-25 08:22:05 UTC
I can't reproduce this either.

Please attach a complete test project we can use to reproduce it ourselves.
Comment 3 softlion 2014-03-10 04:13:49 UTC
Test project attached.
I have this bug since mid january, but didn't took time to create a repro. Here it is.

Comment 4 Lars Wolff 2014-03-17 08:55:52 UTC
I have the same problem.

I noticed that it works in IOS simulator, but not on the actual device.

The PCL is for IOS, Android and Windows phone, and I'm using the NuGET package Microsoft.Net.Http
Comment 5 Vicenç García 2014-03-18 10:54:14 UTC

We've experienced the same issue. What we have done is deactivate the Windows Phone 8 option in the PCL properties and uninstall the Microsoft.Net.Http NuGET package. 

We have been able to do this because our application is only for iOS and Android, but if you have to target Windows Phone 8 also, I don't know how to solve.

Best regards.
Comment 6 Sadik Ali 2014-03-24 11:26:54 UTC
I have checked this issue with the help of project mentioned in comment 3 and able to reproduce this issue. 

I am getting exception when deployed application on device and I click on on "Call Http Request" button. 
Also I noticed that application is working OK on simulator.

Exception log: https://gist.github.com/Mohit-Kheterpal/9742251
Refer screen cast: http://screencast.com/t/9mqZZvNnpUu

Checked With:

Window 7
Visual Studio 2013, 2012
Xamarin.iOS 1.10.47
Comment 7 Rolf Bjarne Kvinge [MSFT] 2014-04-03 09:59:41 UTC
Based on the stack trace this is most likely a dup of #18278.

QA: please verify this once a fix for #18278 has been found/committed.

*** This bug has been marked as a duplicate of bug 18278 ***
Comment 8 Brendan Zagaeski (Xamarin Team, assistant) 2014-06-13 17:43:23 UTC
Updating duplicate.

*** This bug has been marked as a duplicate of bug 20589 ***