Asynchronous Requests with the .NET Kernel

I need a little help to figure out my problem. I used the ASP.NET kernel and I am pretty familiar with this, although the .NET core C # seems to β€œcrash” and exit when I try to execute my asynchronous request request.

I have a method that returns the external IP address of a 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 HC.GetStringAsync. I also tried to set a breakpoint, but this actually doesn't work.

I am trying to call a method using

string Address = await getExternalIP();

Any help is appreciated, I hope I didn’t just miss something.

Thank!

+4
source share
1 answer

.

  • .

:

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

:

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

, , async void async Task ( ).

async void - , . , . , .

async Task - , . void. , , , .

+7

Source: https://habr.com/ru/post/1659112/


All Articles