Bug 45927 - EndGetResponse throws InvalidOperationException after reconnection
Summary: EndGetResponse throws InvalidOperationException after reconnection
Alias: None
Product: iOS
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: XI 10.0 (iOS10)
Hardware: PC Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2016-10-26 16:24 UTC by ostapdemkiv
Modified: 2016-10-27 15:23 UTC (History)
4 users (show)

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


Description ostapdemkiv 2016-10-26 16:24:09 UTC
I have this code: 

 var request = (HttpWebRequest)WebRequest.Create(@"http://www.xamarin.com");
                IAsyncResult result = request.BeginGetResponse(null, null);

                if (!result.AsyncWaitHandle.WaitOne(2000))
                    using (var response = (HttpWebResponse)request.EndGetResponse(result))
                catch (Exception ex)
                    // InvalidOperationException..

Steps to reproduce
1. Launch app.
2. Disable internet connection
3. Run this code (for example attach it to button) 
4. As expected exception is thrown
5. Reconnect to internet
6. Run this code. InvalidOperationException is thrown every time 

This code works good if app is always online. It is reproducing on Xamarin.iOs and Xamarin.Mac.
Comment 1 Vincent Dondain [MSFT] 2016-10-27 12:42:10 UTC
Hey, I just tried with a quick Mac template, added a button and hooked up the code you mentioned above and it works just fine.

Using some Console.WriteLine I'm getting a response with an "ok" status when clicking with an internet connection then a `Exception: System.Net.WebException: Error: ConnectFailure (Network is unreachable) ---> System.Net.Sockets.SocketException: Network is unreachable` when the network is off, then the response with the ok status again when it's turned back on.

Could you please attach 2 things to this bug in order for us to understand what your issue it:

- A test case / project sample (Mac or iOS) that doesn't work for you locally and has the above code.
- Your about information:
The easiest way to get exact version information is to use the "Xamarin Studio" menu, "About Xamarin Studio" item, "Show Details" button and copy/paste the version informations (you can use the "Copy Information" button).
Comment 2 ostapdemkiv 2016-10-27 13:11:47 UTC
Vincent Dondain, thank you for testing so fast!
It is working good the way you described. It is Not working ONLY if first request after  
application launch was offline. I know it sounds strange, but it is reproducing on our Xamarin.iOs, Android and Mac apps.

=== Xamarin Studio Community ===

Version 6.1.1 (build 15)
Installation UUID: 7561d134-f95f-4234-9142-ab7d13a20ffe
	Mono 4.6.1 (mono-4.6.0-branch-c8sr0/ef43c15) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406010005
=== Xamarin.iOS ===

Version: (Xamarin Studio Community)
Hash: ad1cd42
Branch: cycle8-sr0-xi
Build date: 2016-10-03 15:18:44-0400

=== Xamarin.Mac ===

Version: (Xamarin Studio Community)

=== Build Information ===

Release ID: 601010015
Git revision: fa52f02641726146e2589ed86ec4097fbe101888
Build date: 2016-09-22 08:03:02-04
Xamarin addins: 75d65712af93d54dc39ae4c42b21dfa574859fd6
Build lane: monodevelop-lion-cycle8-sr0

=== Operating System ===

Mac OS X 10.12.0
Darwin Ostaps-MacBook-Pro.local 16.0.0 Darwin Kernel Version 16.0.0
    Mon Aug 29 17:56:20 PDT 2016
    root:xnu-3789.1.32~3/RELEASE_X86_64 x86_64
Comment 3 Vincent Dondain [MSFT] 2016-10-27 15:18:41 UTC
Ah indeed I see what you mean, that for the precisions, I did not follow your repro steps careful enough (;

If you launch the app, disable the network right away and click on the button to action the above code, even if you restore the network you'll always get this "Exception: System.Net.WebException: Error: NameResolutionFailure"

Here's a test case for other team members: https://www.dropbox.com/s/onlcxu43usr248y/Bug45927.zip?dl=0

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