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 (show other bugs)
Version: master
Hardware: PC Windows
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-02-21 12:50 UTC by Marcel Bradea
Modified: 2014-06-13 17:43 UTC (History)
7 users (show)

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


Attachments

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

Code:
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


Environment:
- 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)

Example:
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.


http://demo.ovh.eu/fr/ac0ea717fd9fc2b0e6137fc457263be8/
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
Hi,

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 Support) 2014-06-13 17:43:23 UTC
Updating duplicate.

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

Note You need to log in before you can comment on or make changes to this bug.