2

I have written a simple Azure function that gets a name and returns a greeting message (as I am trying to get familiar with Azure functions...) I want to trigger the functions in a simple Console Application that I wrote in C# using Visual Studio. However, I can't get this to work. Everytime I run my code the console opens and immediately closes. I added Console.ReadLine() to try and avoid that but the same thing keeps on happening. Note that the function is working correctly when using the URL in the browser. The code I have written so far is:

static void Main(string[] args)
{
    string nameToSend = "Testo";
    string baseURL = "*URLGOESHERE";
    string urlToInvoke = string.Format("{0}&name={1}", baseURL, nameToSend);
    Run(urlToInvoke);
}

public static async void Run(string i_URL)
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync(i_URL);
    string responseString = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseString);
    Console.ReadLine();
}

Help please! Thanks!

johnny 5
  • 19,893
  • 50
  • 121
  • 195
Niv.R.
  • 63
  • 1
  • 4

1 Answers1

3

Replace your call of Run with Run(urlToInvoke).Wait();. You have to wait for task completion.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • It's not exactly what I needed to do, but the general concept of returning Task so the program waits is what solved the problem. Thanks. – Niv.R. Jun 04 '18 at 19:33