简体   繁体   中英

HttpClient GetAsync Fails

I have been experimenting with building a Windows ConsoleApp to be run as a WebService to sync some data back and forth I have built a project for syncing and when I ran it through my wpf project it seemed to work but not it's not.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using QAQC_DataCommon.Models;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Gettasks();
        }
    public static async void Gettasks()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost/QAQC_SyncWebService/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                var response = await client.GetAsync("Tasks/?username=XXXXXX&LastUpdated=1/1/15");

                if (response.IsSuccessStatusCode)
                {
                    List<QaqcRow> ls = await response.Content.ReadAsAsync<List<QaqcRow>>();

                    foreach (QaqcRow qaqcRow in ls)
                    {
                        Debug.WriteLine(qaqcRow.GetValue("BusinessUnit"));
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

}

It gets up to Var response = Await line when it just exits. No exceptions or any warnings, if I am debugging it just stops.

my output is:

The thread 0x1414 has exited with code 259 (0x103).
The thread 0x16e4 has exited with code 259 (0x103).
The program '[9656] TestApp.vshost.exe' has exited with code 0 (0x0).

My controller from my webservice is as follows:

public IEnumerable<QaqcRow> Index(string username, string lastUpdated)
        {
            return GetFilteredList(username, lastUpdated).OrderBy(x => x.GetValue("FormId"));
        }

I can manually go to the webservice via the link and i get data, but when i use httpclient it just dies.

It's prematurely exiting the program since it doesn't wait for the end of the execution I guess. (see eg https://stackoverflow.com/a/15149840/5296568 )

Change

public static async void Gettasks()

To

public static async Task Gettasks()

Then await the end of the execution.

    static async void Main(string[] args)
    {
        await Gettasks();
    }

Edit: Ahem, so it turns out that Main cannot be async. So maybe for now just confirm that this method get's called properly until the end by blocking the thread.

    static void Main(string[] args)
    {
        Gettasks();
        Console.ReadLine(); //just don't press enter immedietly :) 
    }

Very strange issue, but for me the place I was calling the method that has the http client inside of it was not being awaited:

In my console app I had:

ExampleClass.DoWorkUsingHttpClient()

Changing it to await the method fixed the issue:

await ExampleClass.DoWorkUsingHttpClient()

Additionally my classes were setup like this (sudo code):

public async static ExampleClass 
{
   public async static Task DoWorkUsingHttpClient()
   {
     var httpClient = new HttpClient();
     var result = await httpClient.GetAsync("https://www.example.com");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM