While debugging GraphQL.Client.SendQueryAsync line in the code, the program ends abruptly without any exception

While debugging GraphQL.Client.SendQueryAsync line in the code, the program ends abruptly without any exception


1

I am trying to consume a sample GraphQL api, just to learn it. For that I have created a .Net framework 4.8 based console application, its referencing GraphQL.Client, Abrstractions, Websocket, version 5.1.1.0, etc and Newtownsoft.Json version 13.0.0.0

When I try to debug it, the control disappears from the line graphQLClient.SendQueryAsync

Please help me understand why it disappears and suggest how to fix the issue.

The whole application is below,

using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            // To use NewtonsoftJsonSerializer, add a reference to NuGet package GraphQL.Client.Serializer.Newtonsoft
            Program myObj = new Program();
            myObj.TryMe();
            
        }

        public async Task TryMe()
        {

            var graphQLClient = new GraphQLHttpClient(new GraphQLHttpClientOptions
            {
                EndPoint = new Uri("https://countries.trevorblades.com/")
            }, new NewtonsoftJsonSerializer());
                     
            try
            {

                var request = new GraphQLHttpRequest
                {
                    Query = "query{countries{name,capital,code,currency}}"
                };

                GraphQLResponse<Country> response = await graphQLClient.SendQueryAsync<Country>(request);
                               
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                throw;
            }
                        
        }

    }
    public class Country
    {
        public string name { get; set; }
        public string capital { get; set; }
        public string code { get; set; }
        public string currency { get; set; }
    }
     
}

1 Answer
1


0

I think your main thread is exiting before your awaited call to SendQueryAsync completes.

When the main thread completes, all of its outstanding other threads are terminated. Your asynchronous function call is executing on one of those other threads, and it’s not finishing before the main thread does. So it gets destroyed before the call completes and you get your data back in the main thread.

See Debugger stops after async HttpClient.GetAsync() call in visual studio.



Leave a Reply

Your email address will not be published. Required fields are marked *