Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. Xamarin Forms Async Call Never Returns

Xamarin Forms Async Call Never Returns

Scheduled Pinned Locked Moved Mobile
jsoncsharpasp-netmobilevisual-studio
7 Posts 4 Posters 30 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm trying to learn to call an Asp.Net Web API from a Xamarin Forms app. I'm testing my app using my phone plugged into my Dev PC running from VS 2019. Here's my code so far:

    private void Button_Clicked(object sender, EventArgs e)
    {
    CustomerEntity customer = Test(1).Result;
    }
    public async Task Test(int id)
    {
    var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";
    using (var client = new HttpClient())
    {
    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await client.GetAsync(url); // Nothing happens after this!!
    
        CustomerEntity cust = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
         
        return cust;
    }
    

    }

    I can paste the URI into a browser and get results. When I click the button on my app that calls the above, I get no response after the GetAsync. Anyone have any idea what's going on or how to debug this? Thanks

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    A 1 Reply Last reply
    0
    • K Kevin Marois

      I'm trying to learn to call an Asp.Net Web API from a Xamarin Forms app. I'm testing my app using my phone plugged into my Dev PC running from VS 2019. Here's my code so far:

      private void Button_Clicked(object sender, EventArgs e)
      {
      CustomerEntity customer = Test(1).Result;
      }
      public async Task Test(int id)
      {
      var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";
      using (var client = new HttpClient())
      {
      client.BaseAddress = new Uri(url);
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

          var response = await client.GetAsync(url); // Nothing happens after this!!
      
          CustomerEntity cust = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
           
          return cust;
      }
      

      }

      I can paste the URI into a browser and get results. When I click the button on my app that calls the above, I get no response after the GetAsync. Anyone have any idea what's going on or how to debug this? Thanks

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      Quote:

      var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";

      Unless your Android (or the mobile device) has a local server running on the machine that can respond to a localhost:44302 request, you will not receive a response and request will likely timeout and fail. There are several ways to connect to a localhost based website: 1. You have a physical Android device that you are using for testing 1. You are using a virtualized device for testing If you have a physical device, then try to send the request to the host (your development machine)'s IP address with the port number; for example, change localhost:44302 to 192.168.1.17:44302 (assuming your machine's address on the same network is 192.168.1.17) If you are using the virtualized then this gets complicated and depends on several factors; what network setting is being used, which OS you are using, does the virtualization software support host-connections, etc. etc. Oh, and the best of these is to use an online proxy that can tunnel your Android device with the local machine; something like NgRok can help here. [ngrok - secure introspectable tunnels to localhost](https://ngrok.com/)

      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

      K A 2 Replies Last reply
      0
      • A Afzaal Ahmad Zeeshan

        Quote:

        var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";

        Unless your Android (or the mobile device) has a local server running on the machine that can respond to a localhost:44302 request, you will not receive a response and request will likely timeout and fail. There are several ways to connect to a localhost based website: 1. You have a physical Android device that you are using for testing 1. You are using a virtualized device for testing If you have a physical device, then try to send the request to the host (your development machine)'s IP address with the port number; for example, change localhost:44302 to 192.168.1.17:44302 (assuming your machine's address on the same network is 192.168.1.17) If you are using the virtualized then this gets complicated and depends on several factors; what network setting is being used, which OS you are using, does the virtualization software support host-connections, etc. etc. Oh, and the best of these is to use an online proxy that can tunnel your Android device with the local machine; something like NgRok can help here. [ngrok - secure introspectable tunnels to localhost](https://ngrok.com/)

        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        I'm using my phone plugged into my PC. When I paste this into the browser I get data back:

        "https://localhost:44302/api/Customer/Get?Id=1"

        When I paste this into the browser

        "https://192.168.1.149:44302/api/Customer/Get?Id=1"

        I get

        Bad Request - Invalid Hostname

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        A 1 Reply Last reply
        0
        • K Kevin Marois

          I'm using my phone plugged into my PC. When I paste this into the browser I get data back:

          "https://localhost:44302/api/Customer/Get?Id=1"

          When I paste this into the browser

          "https://192.168.1.149:44302/api/Customer/Get?Id=1"

          I get

          Bad Request - Invalid Hostname

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          A Offline
          A Offline
          Afzaal Ahmad Zeeshan
          wrote on last edited by
          #4

          Quote:

          I'm using my phone plugged into my PC.

          I don't think that unless you also share the network via USB interface, you can access the PC's `localhost`.

          Quote:

          When I paste this into the browser I get data back: "https://localhost:44302/api/Customer/Get?Id=1"

          Because your "PC browser" has a process that is internally listening on the `localhost` (`127.0.0.1`) @ `44302`. So it works.

          Quote:

          When I paste this into the browser "https://192.168.1.149:44302/api/Customer/Get?Id=1"

          This would "only" work if you have a machine or a process that is listening on 192.168.1.149 @ 44302. This is more likely a networking issue rather than an Android/PC problem. And, when you are on the same machine, you should always use `127.0.0.1` (aka `localhost`) instead of the machine's own private IP address. Oh, one more tip, try only http:// and not the https://, or you will end up running in the SSL errors and you might assume the machine is down.

          The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

          K 1 Reply Last reply
          0
          • A Afzaal Ahmad Zeeshan

            Quote:

            I'm using my phone plugged into my PC.

            I don't think that unless you also share the network via USB interface, you can access the PC's `localhost`.

            Quote:

            When I paste this into the browser I get data back: "https://localhost:44302/api/Customer/Get?Id=1"

            Because your "PC browser" has a process that is internally listening on the `localhost` (`127.0.0.1`) @ `44302`. So it works.

            Quote:

            When I paste this into the browser "https://192.168.1.149:44302/api/Customer/Get?Id=1"

            This would "only" work if you have a machine or a process that is listening on 192.168.1.149 @ 44302. This is more likely a networking issue rather than an Android/PC problem. And, when you are on the same machine, you should always use `127.0.0.1` (aka `localhost`) instead of the machine's own private IP address. Oh, one more tip, try only http:// and not the https://, or you will end up running in the SSL errors and you might assume the machine is down.

            The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            This is all confusing to me. All I want to do is write a simple test Xamarin Forms app that connects to a Web API. I'm testing on my phone which is connected via USB to my PC. WHat's the right way to do this? Can you point me to any resources for consuming a web API from Xamarin Forms?

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            M 1 Reply Last reply
            0
            • K Kevin Marois

              This is all confusing to me. All I want to do is write a simple test Xamarin Forms app that connects to a Web API. I'm testing on my phone which is connected via USB to my PC. WHat's the right way to do this? Can you point me to any resources for consuming a web API from Xamarin Forms?

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              I think if you use the emulator in vs2019 you may be able to access localhost (a guess). The issue is that the phone is a separate device without the infrastructure to access you PCs IIS server.

              Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

              1 Reply Last reply
              0
              • A Afzaal Ahmad Zeeshan

                Quote:

                var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";

                Unless your Android (or the mobile device) has a local server running on the machine that can respond to a localhost:44302 request, you will not receive a response and request will likely timeout and fail. There are several ways to connect to a localhost based website: 1. You have a physical Android device that you are using for testing 1. You are using a virtualized device for testing If you have a physical device, then try to send the request to the host (your development machine)'s IP address with the port number; for example, change localhost:44302 to 192.168.1.17:44302 (assuming your machine's address on the same network is 192.168.1.17) If you are using the virtualized then this gets complicated and depends on several factors; what network setting is being used, which OS you are using, does the virtualization software support host-connections, etc. etc. Oh, and the best of these is to use an online proxy that can tunnel your Android device with the local machine; something like NgRok can help here. [ngrok - secure introspectable tunnels to localhost](https://ngrok.com/)

                The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                A Offline
                A Offline
                Angelina Brown
                wrote on last edited by
                #7

                I also face the same issue. Looking forward to get the right one.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups