Async requests with .NET core

16,829

Your proposed solution is not good at all.

  1. It is synchronous
  2. It can cause a deadlock

As an alternative, try this approach:

private async Task<string> GetExternalIP()
{
    using (HttpClient client = new HttpClient())
        return await client.GetStringAsync("https://api.ipify.org/");
}

Your calling method should be asynchronous too:

public async Task CallingMethod()
{
     // ...
     string address = await GetExternalIP();
     // ...
}

The reason it was not working for you before was caused by the use of async void instead of async Task (guessed this by the comments).

async void is an asynchronous method that cannot be awaited. That means, you just call it and forget about it. You won't catch any exception, you won't get any return value.

async Task is an awaitable asynchronous method that does not return anything. It is the asynchronous counterpart for a void synchronous method. Furthermore, since it is awaitable, you'll also be able to catch any exception that may rise on the asynchronous code.

Share:
16,829

Related videos on Youtube

Sasha
Author by

Sasha

Full stack dev

Updated on September 14, 2022

Comments

  • Sasha
    Sasha over 1 year

    I need a little help to find out my problem. I've used ASP.NET core and i'm fairly familiar with that, although .NET core C# seems to be "crashing" and exiting when trying to make my async request.

    I have a method that returns the external IP of the system

    private async Task<string> getExternalIP()
        {
            using (System.Net.Http.HttpClient HC = new System.Net.Http.HttpClient())
            {
                return await HC.GetStringAsync("https://api.ipify.org/");
            }
    
        }
    

    This should work, but it exits when it reaches the HC.GetStringAsync. I've also tried putting a breakpoint on it but it doesn't actually run.

    I'm trying to call the method by using

    string Address = await getExternalIP();
    

    Any help is thankful, hopefully i'm not just overlooking something.

    Thanks!

  • Sasha
    Sasha over 7 years
    I originally had something similar to this, but the problem is the "CallingMethod" ends up not returning and the application exits. No exception, no message.
  • Matias Cicero
    Matias Cicero over 7 years
    @ProNinjaCat Because you were using async void and not async Task. You'll also need to await CallingMethod() from whoever calls it.
  • Sasha
    Sasha over 7 years
    I have the async Task running await getExternalIP() and the getExternalIP() method the exact same, but it still doesn't execute.
  • Matias Cicero
    Matias Cicero over 7 years
    @ProNinjaCat Are you awaiting CallingMethod() too?
  • Sasha
    Sasha over 7 years
    That's inside my Main(), which doesn't allow Async in it's signature
  • mason
    mason over 7 years
    @ProNinjaCat Then that's why it never ran. You never awaited it. If you want an async console app, see this question.
  • Sasha
    Sasha over 7 years
    @mason This was the problem, thanks for figuring this out otherwise I would of probably kept my solution!
  • Matias Cicero
    Matias Cicero over 7 years
    @ProNinjaCat Glad we came to an understanding. If you consider this answer helpful to you (and to others), please mark it as accepted.