cannot call apis from console apication

Eduardo Gomez 3,416 Reputation points
2022-08-24T21:51:18.573+00:00

for some weird reason, I started a console app, to do something quickly and for some reason, I am no longer able to a call APIs

I made a method, and when it reaches a certain line, it exits the app

   static void Main(string[] args)  
    {  
        call();  
    }  
  
    private static async void call()  
    {  
        await ApiTest();  
    }  
  
    private static async Task<Result> ApiTest()  
    {  
        var url = "https://pokeapi.co/api/v2/pokemon?limit=10228";  
        using (var client = new HttpClient())  
        {  
            var res = await client.GetAsync(url); // reach this line and exit the app inmidietly  
            if (res.IsSuccessStatusCode)  
            {  
                var p = res.Content.ReadFromJsonAsync<List<Result>>(); // I tried to put a breakpoint here and nothing  
  
            }  
        }  
        return null;  
    }  
}  

I tried with other paid and get the same result

I am trying to return a list of names, so I have this class

public class Result  
{  
    public string name  
    {  
        get; set;  
    }  
    public string url  
    {  
        get; set;  
    }  
}  
  
public class RootObject  
{  
    public IList<Result> results  
    {  
        get; set;  
    }  
}  
  

I never had any problems with APIs, what is going on

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,155 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 111.5K Reputation points
    2022-08-24T23:26:32.317+00:00

    Try these changes:

    static void Main(string[] args)  
    {  
       call().Wait( );  
    }  
      
    private static async Task call( )  
    {  
        await ApiTest( );  
    }  
      
    private static async Task<Result> ApiTest( )  
    {  
        var url = "https://pokeapi.co/api/v2/pokemon?limit=10228";  
        using( var client = new HttpClient( ) )  
        {  
            var res = await client.GetAsync( url );  
            if( res.IsSuccessStatusCode )  
            {  
                var p = res.Content.ReadFromJsonAsync<RootObject>( );   
      
            }  
        }  
        return null;  
    }  
    

0 additional answers

Sort by: Most helpful