my C# console application exits when making post request

  Uncategorised

I want to post a json object, and the code I’ve made is like this

public static async Task<HttpResponseMessage> MakeAsyncPostJsonRequest<T>(string url, T obj)
    {
        using (var client = new HttpClient()
        {
            Timeout = new TimeSpan(0, 5, 0),
            BaseAddress = new Uri(url)
        })
        {
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type: application/x-www-form-urlencoded", "application/json");
            var content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
            var result = await client.PostAsync(url, content);
            return result;
        }
    }

I’m in the debuggin process and every time I step into the

var result = await client.PostAsync(url, content);

part the program always exits "with code 0". I confirmed the url and json content and they’re perfectly fine.
The weirder part is I have the exact same code in another program, and it works fine there, both using the same visual studio in the same pc so I don’t know why this is happening.

To add context, I’m using this to login and get an access token from the response. I call this method in this way

public static async Task<string> GetNewAccessToken()
        {

            var loginDetails = new LoginDto(ConfigurationManager.AppSettings["email"], ConfigurationManager.AppSettings["password"]);
            var response = await ServiceHelper.MakeAsyncPostJsonRequest(ConfigurationManager.AppSettings["LoginUrlLocal"], loginDetails);
            var responseBody = JsonConvert.DeserializeObject<LoginResponseDto>(await response.Content.ReadAsStringAsync());
            return responseBody.AccessToken;
        }

Source: Visual Studio Questions

LEAVE A COMMENT