HttpClient.SendAsync works fine on iOS simulator but throws an exception when debugging on device

System.AshSystem.Ash AUMember ✭✭

As per title. Debugging on iOS simulator works fine.
This is the exception I get after a reasonable wait time, while debugging on an iPhone:

MakeApiRequestAsync error: System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
  at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x0003b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/mcs/class/System/System.Net.Sockets/SocketAsyncResult.cs:134 
  at System.Net.Sockets.Socket.EndConnect (System.IAsyncResult asyncResult) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/mcs/class/System/System.Net.Sockets/Socket.cs:1150 
  at System.Net.Sockets.SocketTaskExtensions+<>c.<ConnectAsync>b__2_1 (System.IAsyncResult asyncResult) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/mcs/class/System/System.Net.Sockets/SocketTaskExtensions.cs:35 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00019] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs:538 
--- End of stack trace from previous location where exception was thrown ---

  at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x00175] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:169 
   --- End of inner exception stack trace ---
  at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x0023b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:188 
  at System.Net.WebConnection.InitConnection (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x000cc] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:259 
  at System.Net.WebOperation.Run () [0x00052] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/mcs/class/System/System.Net/WebOperation.cs:268 
  at System.Net.WebCompletionSource`1[T].WaitForCompletion () [0x00094] in <1f9da90787fa4aaeae29e27725405a37>:0 
  at System.Net.HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task`1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func`1[TResult] aborted, System.Threading.CancellationTokenSource cts) [0x000f8] in <1f9da90787fa4aaeae29e27725405a37>:0 
  at System.Net.Http.HttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x0029d] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:392 

Exact same code for both. Using the IP address of my machine instead of localhost in the request URI.

Thought it might be ATS so I disabled it using:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Still no luck.

Any ideas?

Environment:

Visual Studio Community 2019 for Mac:
  Version 8.0.4 (build 0)

Mono Framework MDK:
  Runtime:
      Mono 5.18.1.3 (2018-08/fdb26b0a445) (64-bit)
      Package version: 518010003

Xamarin.iOS:
  Version: 12.8.0.0 (Visual Studio Community)

Xcode:
  Version 10.2.1 (10E1001)

Answers

  • System.AshSystem.Ash AUMember ✭✭

    Just tested on an Android device and that works fine too.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Have you made sure that your machine's IP address can be accessed by your iPhone?

  • System.AshSystem.Ash AUMember ✭✭

    @LandLu It had worked without problems on other projects, so I'm assuming that is not the issue.

Sign In or Register to comment.