Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async requests with .NET core

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!

like image 848
Sasha Avatar asked Oct 27 '16 19:10

Sasha


People also ask

Should .NET Core API be async?

ASP.NET Core apps should be designed to process many requests simultaneously. Asynchronous APIs allow a small pool of threads to handle thousands of concurrent requests by not waiting on blocking calls. Rather than waiting on a long-running synchronous task to complete, the thread can work on another request.

How does async work in .NET Core?

Asynchronous programming allows you to write programs that don't block on each statement or instruction, meaning the computer can move on to other tasks before waiting for previous tasks to finish. As a result, asynchronous programming enables you to build applications that are more scalable and responsive.

Should I use async EF core?

Generally speaking, if there are asynchronous APIs, then you should use them for new code. Asynchronous code frees up the calling thread. If your application is a GUI application, this can free up the UI thread; if your application is a server application, this can free up threads to handle other requests.

How use async await in NET Core?

The await keyword performs an asynchronous wait on its argument. It does that in several steps. The first thing it does is to check whether the operation is already complete. If it is, it will continue the method execution synchronously.


1 Answers

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.

like image 85
Matias Cicero Avatar answered Sep 21 '22 21:09

Matias Cicero