title: Web service request gets stuck while switching between application in Xamarin iOS
body: <p>We are trying to make a call to web service (nonsecure i.e. HTTP) in <code>Xamarin.iOS</code>. As it is an <code>http</code> URL we have added <code>NSAppTransportSecurity</code> to bypass the secure access.</p>
  <p>Currently, we are using <code>HttpClient</code> with <code>NSUrlSessionHandler</code> so that <code>ATS config</code> specified in <code>info.plist</code> gets applied.</p>
  <pre><code>client = new HttpClient(new NSUrlSessionHandler());
  </code></pre>
  <p><strong>Problem #1</strong></p>
  <p>Now, the issue is whenever we are firing the web service request and after that, if we switch to a different app and come back to our app, the request gets stuck and does not return anything. if we stay on our app, it returns the response successfully.</p>
  <p><strong>Problem #2</strong></p>
  <p>In order to resolve <code>Problem#1</code> issue of switching app, we tried <code>client = new HttpClient(new CFNetworkHandler());</code> i.e. using <code>CFNetworkHandler</code> and that resolves our issue but this eventually creates another issue of accessing <code>http</code> URL. As we have used <code>CFNetworkHandler</code> , configurations related to <code>NSAppTransportSecurity</code> in <code>info.plist</code> gets ignored. As <a href="https://developer.apple.com/documentation/security/preventing_insecure_network_connections" rel="nofollow noreferrer">Apple Developer Document says</a></p>
  <blockquote>
  <p>ATS doesn’t apply to calls your app makes to lower-level networking
  interfaces like the Network framework or CFNetwork.</p>
  </blockquote>
  <p>Error that we get while calling webservice with <code>CFNetwork</code> is as follows and it looks like <code>info.plist</code> config is getting ignored</p>
  <pre><code>StatusCode: 505, ReasonPhrase: 'HTTP/1.1 505 HTTP Version Not Supported', Version: 1.1, Content: System.Net.Http.CFContentStream, Headers:
  {
    Date: Fri, 03 Jul 2020 06:15:19 GMT
    Connection: close
    Server: Microsoft-HTTPAPI/2.0
    Content-Type: text/html; charset=us-ascii
    Content-Length: 350
  }
  </code></pre>
  <p>However, on the other end,<a href="https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/ats#config" rel="nofollow noreferrer">Microsoft Xamarin Says</a></p>
  <pre><code>App Transport Security does not apply to Xamarin apps using Managed HTTPClient implementations. It applies to connections using CFNetwork HTTPClient implementations or NSURLSession HTTPClient implementations only.
  </code></pre>
  <p>So, if we use <code>NSUrlSessionHandler</code> the request got stuck while switching app and if we use <code>CFNetworkHandler</code> then ATS won't allow to access the services.</p>
  <p>ATS Configurations are as follows</p>
  <ol>
  <li><p>set <code>NSAppTransportSecurity</code> with <code>NSAllowsArbitraryLoads</code> set to <code>YES</code></p>
  </li>
  <li><p>set <code>NSExceptionDomains</code> with following parameters</p>
  <p>a. <code>naaxdev.cloudapp.net</code></p>
  <p>b. <code>NSIncludesSubdomains</code> set to <code>YES</code></p>
  <p>c. <code>NSExceptionAllowsInsecureHTTPLoads</code> set to <code>YES</code></p>
  <p>d. <code>NSExceptionRequiresForwardSecrecy</code> set to <code>NO</code></p>
  </li>
  </ol>