Bug 18278 - HttpClient broken (xamarin.ios 1.10.39 / 7.0.7.2)
Summary: HttpClient broken (xamarin.ios 1.10.39 / 7.0.7.2)
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Visual Studio Extensions
Classification: Xamarin
Component: General ()
Version: 1.10.x
Hardware: PC Windows
: High critical
Target Milestone: 3.1
Assignee: Jose Miguel Torres
URL:
Depends on:
Blocks:
 
Reported: 2014-03-10 07:14 UTC by softlion
Modified: 2016-08-17 09:40 UTC (History)
20 users (show)

Tags:
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 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:
Status:
RESOLVED NOT_REPRODUCIBLE

Description softlion 2014-03-10 07:14:41 UTC
Put this simple code in a hello world ios7 project.
Run it on simulator: it works.
Run it on device: it fails with an exception.


        public Tuple<bool,string> Request(string url)
        {
            string result;
            try
            {
                using (var wc = new HttpClient())
                {
                    result = wc.GetStringAsync(url).Result;
                    return Tuple.Create(true, result);
                }
            }
            catch (Exception e)
            {
                return Tuple.Create(false,e.ToString());
            }
        }

See Bug 18036 where i added a repro project in the comments.
Comment 1 Ram Chandra 2014-03-10 10:04:08 UTC
I have checked this issue and able to reproduce this issue.

Steps to reproduce:

1. Create an  an "Hello WorldApplication"  iOS Project on VS2013.
2. Put the following simple code in "MyViewController.cs" 

        public Tuple<bool,string> Request(string url)
        {
            string result;
            try
            {
                using (var wc = new HttpClient())
                {
                    result = wc.GetStringAsync(url).Result;
                    return Tuple.Create(true, result);
                }
            }
            catch (Exception e)
            {
                return Tuple.Create(false,e.ToString());
            }
        }

3. Build and deploy the project.
4. After deploying the app. It fails with an exception.


Environment :

VS2013
Xamarin.iOS 1.10.39
iOS device version 7.1

Debug Log : https://gist.github.com/Mohit-Kheterpal/d992d686eaa191b35c2f

Mac Server Log : https://gist.github.com/Mohit-Kheterpal/f0cdf24ad15eda09ba23

Mtbserver logs :https://gist.github.com/Sadik1/e4b5415023a9a207ecea
Comment 2 Rolf Bjarne Kvinge [MSFT] 2014-03-12 06:32:29 UTC
@Ram: can you try running the same project from Xamarin Studio on the Mac? And if it fails the same way, please attach the solution.
Comment 3 softlion 2014-03-23 06:31:20 UTC
I confirm the workaround.

It also fails in debug mode when Linker is set to "link SDK only".
One workaround is to set it to "don't link". Run and it will work ok.
SO THIS IS A PROBLEM WITH LINKING too aggressive.

Workaround for debug mode with "link to SDK only". Not tested in release mode, but should work:
- in PCL remove HttpClient nuget package
- retarget the PCL to .NET 4.5, monotouch, monodroid (remove windows phone 8). PCL profile will be 7.

Rebuild, run. it works.
Comment 4 softlion 2014-03-23 17:16:46 UTC
The workaround does not work on device, in debug/release mode.

"Attempting to jit compile method" in System.Lightup.TryCall(HttpWebRequest,bool).
Comment 5 softlion 2014-03-24 03:53:37 UTC
Finally i have a working workaround. Use ModernHttpClient : register a singleton for HttpClient class in ios/android, then get the singleton instance from your PCL. Use the "definition" of HttpClient class by adding its nuget package in the PCL only.

it works in all cases.
Comment 6 Sebastien Pouliot 2014-04-02 20:15:46 UTC
> System.Lightup.TryCall

^ that's not from XI BCL/PCL assemblies. It sounds like MTVS copied MS System.Net.Http.dll to the Mac host, instead of relying to the one that XI provides. That definitively won't work (and unrelated to the linker).


@Ram please see comment #2 - that will confirm where the issue is.
Comment 8 Sebastien Pouliot 2014-04-03 08:13:35 UTC
Thanks. -> MTVS
Comment 9 Rolf Bjarne Kvinge [MSFT] 2014-04-03 09:59:41 UTC
*** Bug 17936 has been marked as a duplicate of this bug. ***
Comment 10 vol714 2014-04-10 06:28:36 UTC
For those in need of a workaround, setting CopyLocal to false for the System.Net.Http.* assembly references in both my PCL (profile 78 with Microsoft.Net.Http NuGet package) and iOS projects solved the problem for me. 

Caution: NuGet will set CopyLocal back to true when upgrading a NuGet package (and possibly also mess with your binding redirects in app.config). In fact, that's what caused the issue in my case - the code used to work when built with VS before I upgraded Microsoft.Net.Http through NuGet. (The code always did work when built with XS - comment #6 about copying references to the Mac put me on the right track.)

The more I think about it, the more it seems a NuGet issue to me.
Comment 11 Brendan Zagaeski (Xamarin Team, assistant) 2014-05-08 19:39:51 UTC
The test case attached to this bug runs without error for me on:

- Xamarin.iOS 7.2.2 (Mac)
- Xamarin.iOS for VS 1.12.275 (Windows)


I suspect the original Description was missing an important detail: the `HttpClient` code was being run from within a .NET 4.0 PCL via the "Microsoft.Net.Http" NuGet package. This is hinted at later in comment 3. As far as I can tell, the system versions of System.Net.Http do not reference "Lightup" at all.

In particular, note that the logs from comment 2 do not contain "System.Lightup.TryCall".


Based on bug #18036, I believe the correct test case for this bug is:
https://bugzilla.xamarin.com/show_bug.cgi?id=18036#c2


Since the links for the test cases on bug #18036 are no longer working, I can't confirm whether both test cases on that bug are actually duplicates of each other.


But even without that, I think we can safely clean up these bugs as follows:

1. Make bug #17936 its own bug again (instead of a duplicate of bug #18036).


2. Reassign bug #17396 to MTVS. I suspect the corresponding test case is https://bugzilla.xamarin.com/show_bug.cgi?id=18036#c4. I also suspect that this is equivalent to the updated test case from bug #15290, except that it's missing the required `app.config`.


3. Mark this bug (bug #18278) as a duplicate of bug #18036.


If someone still has a copy of the test case from https://bugzilla.xamarin.com/show_bug.cgi?id=18036#c2, you can check if adding the `app.config` from [1] to the app project fixes the problem. If so, then bug #18036 can be marked as a duplicate of bug #17936.

[1] https://gist.githubusercontent.com/jamesmontemagno/9403574/raw/app.config
Comment 12 Brendan Zagaeski (Xamarin Team, assistant) 2014-05-08 19:42:38 UTC
"Reassign bug #17396 to MTVS."

Should be:

"Reassign bug #17936 to MTVS."
Comment 13 Jose Miguel Torres 2014-05-21 10:42:29 UTC
I have tried to reproduce this bug using the very latest binaries for Xamarin.iOS VS plugin using the attached project and it works. I have also tried to create a new project using PCL and it works as well.

So, could somebody point me to the test case mentioned here (https://bugzilla.xamarin.com/show_bug.cgi?id=18036#c2) or any other one that might trigger this bug?
Comment 14 Jose Miguel Torres 2014-06-04 05:33:32 UTC
Tested with master repo and XVS 3.0.54 and unable to reproduce. Looks like it's working right now. Please, reopen it if something is wrong.

Thanks!!
Comment 15 Allan Ritchie 2014-07-16 08:28:18 UTC
I'm getting this with the latest stable and beta releases.  I've deleted obj/bins everywhere.  I've set "copy local" to false on these libraries, generic type sharing is enabled, linker is disabled.  Nothing is working.
Comment 16 Rolf Bjarne Kvinge [MSFT] 2014-07-16 08:33:35 UTC
@aritchie: can you attach the full build log?
Comment 17 Allan Ritchie 2014-07-16 16:21:51 UTC
I finally found the fix.  I had to manually delete the cache that is kept by mtbserver.  Once I rebuilt, everything started working
Comment 18 Dibran Mulder 2016-08-17 09:40:45 UTC
I also ran into this issue today. I installed the Microsoft.Net.Http NuGet package in a PCL project of mine. The iOS project referenced the PCL project and performed some https calls via the PCL project. This worked great in the simulator :)

However once I tested it on a real device to whole bunch collapsed. I got the "Attempting to JIT compile method with --aot-only" Exception. I solved this by installing the NuGet package also on my iOS project. The wrong version of the dll got loaded somehow. It looks like the net45 version of System.Net.Http is used instead of the Mono iOS version.