简体   繁体   English

使用C#调用Azure函数

[英]Invoking Azure function using C#

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. 我编写了一个简单的Azure函数,该函数获取名称并返回问候消息(因为我正试图熟悉Azure函数...)我想在我使用Visual Studio以C#编写的简单控制台应用程序中触发这些函数。 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. 我添加了Console.ReadLine()来尝试避免这种情况,但是同一件事不断发生。 Note that the function is working correctly when using the URL in the browser. 请注意,在浏览器中使用URL时,该功能正常工作。 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! 谢谢!

Replace your call of Run with Run(urlToInvoke).Wait(); Run(urlToInvoke).Wait();代替对Run的调用Run(urlToInvoke).Wait(); . You have to wait for task completion. 您必须等待任务完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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