none
Await works in console but does not in c# Library project RRS feed

  • Question

  • Hello all,

    I was working with HttpClient.PostAsync and couldn't test it line by line with visual studio 2017 it blocks in this line without any exception it just stops the test.

    var response = await HttpClient.PostAsync(url, encodedContent).ConfigureAwait(false);

    He is part of my code

                

    [TestMethod()]
            public void GetData()
            {

              /// Other code

                MyClass object = new object(Params);
                object.Methode1();

            }

    public void Methode1()
            {
                //Other code
                    Task.Run(() => DownloadPageAsync(param1, param2));

                }

    private async void DownloadPageAsync(string param1, string param2)
            {
     //Other code for parameters
                HttpClient HttpClient = new HttpClient();
                string page ="URL HERE";
                var url = page;

                var encodedContent = new FormUrlEncodedContent(parameters);
                var response = await HttpClient.PostAsync(url, encodedContent).ConfigureAwait(false);

    So what I did is that I tried to run nearly the same code and it did work in Console app.

    Can someone explain to me what i am doing wrong please ?




    • Edited by ExaCsharp Wednesday, March 27, 2019 3:32 PM
    Wednesday, March 27, 2019 3:27 PM

Answers

  • Your test method (DownloadPageAsync) needs to return Task otherwise the test runner doesn't know to stop and wait for it. Note that in general async methods need to return Task.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by ExaCsharp Monday, April 1, 2019 3:06 PM
    Wednesday, March 27, 2019 5:05 PM
    Moderator
  • Hello thanks for your reply, actually it wasn't a problem related only to  return Task  it was working in console because I had a Console.ReadLine blocking my console so I can read the output which gave time to my code to execute. But i didn't have that in my Library code.

    Adding Wait() to my code solved my problem hope this helps someone .

    static void Main(string[] args)
            {

    ///code

                test(params).Wait();

    }

     static async Task<int> test(Myvar pv)
            {
    ///code

    Class c = new Class() ;

     int i = await c.Methode1();

    }

    public async Task<int> Methode1()
                {

    ///code

                     await Task.Run(() => DownloadPageAsync(params));

    }

    public async Task<int> DownloadPageAsync(params)
             {

    ///code

    }

    • Marked as answer by ExaCsharp Monday, April 1, 2019 3:06 PM
    Monday, April 1, 2019 3:05 PM

All replies

  • Your test method (DownloadPageAsync) needs to return Task otherwise the test runner doesn't know to stop and wait for it. Note that in general async methods need to return Task.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by ExaCsharp Monday, April 1, 2019 3:06 PM
    Wednesday, March 27, 2019 5:05 PM
    Moderator
  • Hello thanks for your reply, actually it wasn't a problem related only to  return Task  it was working in console because I had a Console.ReadLine blocking my console so I can read the output which gave time to my code to execute. But i didn't have that in my Library code.

    Adding Wait() to my code solved my problem hope this helps someone .

    static void Main(string[] args)
            {

    ///code

                test(params).Wait();

    }

     static async Task<int> test(Myvar pv)
            {
    ///code

    Class c = new Class() ;

     int i = await c.Methode1();

    }

    public async Task<int> Methode1()
                {

    ///code

                     await Task.Run(() => DownloadPageAsync(params));

    }

    public async Task<int> DownloadPageAsync(params)
             {

    ///code

    }

    • Marked as answer by ExaCsharp Monday, April 1, 2019 3:06 PM
    Monday, April 1, 2019 3:05 PM