title: Asp.net core wed API on Xamarin Forms
body: <p>This might be a not so wise question, but I am having hard times to understand the principles. I am working on Xamarin Forms project where I would like to use Azure SQL. I have watched several tutorials and understood that I need to create an Asp.net Core Web API for providing SQL operations. I have created such an API and tested it with Swagger on my computer. It is able to read and write into my Azure SQL database. I have my codes in Xamarin Forms for accessing this API service, but I am having hard times understanding how to proceed further?</p>
  <p>Can I run this API somehow in Android directly? I mean can this API be part of my Xamarin Forms application and run inside Android or I have to upload this Asp.net Core Web API application into Azure and then access it with https link from my Xamarin Forms application? Should it be hosted on web or can it run directly inside Andoird like it runs on my Windows PC with access from <code>localhost:7250/api/datarecord</code>?</p>
  <p>According to my understanding it can run on Android?</p>
  <p>I have added this into my App.xaml.cs in Xamarin Forms:</p>
  <pre><code>public static string AzureBackendUrl =
  DeviceInfo.Platform == DevicePlatform.Android ? &quot;http://10.0.2.2:5000&quot; : &quot;http://localhost:5000&quot;;
  </code></pre>
  <p>Then I have this Service in Xamarin Forms:</p>
  <pre><code>  public class AzureDataStore : IRepository&lt;DataRecord&gt;
    {
      HttpClient client;
      IEnumerable&lt;DataRecord&gt; items;
      ILogger&lt;AzureDataStore&gt; logger;
  
      public AzureDataStore(ILogger&lt;AzureDataStore&gt; logger = null, IHttpClientFactory httpClientFactory = null)
      {
        this.logger = logger;
        client = httpClientFactory == null ? new HttpClient() : httpClientFactory.CreateClient(&quot;AzureWebsites&quot;);
  
        if (httpClientFactory == null)
          client.BaseAddress = new Uri($&quot;{App.AzureBackendUrl}/&quot;);
  
        items = new List&lt;DataRecord&gt;();
      }
  
      bool IsConnected =&gt; Connectivity.NetworkAccess == NetworkAccess.Internet;
      public async Task&lt;IEnumerable&lt;DataRecord&gt;&gt; GetAll()
      {
        logger?.LogCritical(&quot;Getting items!!! Wow!&quot;);
        if (IsConnected)
        {
          string json = await client.GetStringAsync($&quot;api/DataRecord&quot;);
          items = await Task.Run(() =&gt; JsonConvert.DeserializeObject&lt;IEnumerable&lt;DataRecord&gt;&gt;(json));
        }
  
        return items;
      }
  
      ...
  </code></pre>
  <p>However once I am running this all together on my Android phone, my application is not able to communicate with WEB API for some reason.</p>