8

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!

15
  • You're probably getting an exception. Commented Oct 27, 2016 at 19:26
  • @SLaks I've re-enabled all exception notices. No exception is being thrown. Commented Oct 27, 2016 at 19:26
  • Are you using async void? Commented Oct 27, 2016 at 19:27
  • @SLaks yes for the method that calls it Commented Oct 27, 2016 at 19:29
  • @ProNinjaCat Exception handling with async void Commented Oct 27, 2016 at 19:30

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
@ProNinjaCat Because you were using async void and not async Task. You'll also need to await CallingMethod() from whoever calls it.
I have the async Task running await getExternalIP() and the getExternalIP() method the exact same, but it still doesn't execute.
@ProNinjaCat Are you awaiting CallingMethod() too?
@ProNinjaCat Then that's why it never ran. You never awaited it. If you want an async console app, see this question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.