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,648 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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;
}