gpt4 book ai didi

azure - 为什么 HttpClient PostAsJsonAsync 退出 Azure Web 作业而不运行其后的代码?

转载 作者:行者123 更新时间:2023-12-02 06:21:42 25 4
gpt4 key购买 nike

我有一个使用 Azure SDK 构建的 Azure Web 作业,其唯一的工作是调用 Web 服务 (Web API),然后根据返回值(类)记录响应。问题是,一旦它调用 HttpClient PostAsJsonAsync 方法来调用服务,它就会退出 Web 作业,而不执行任何响应处理。我的代码是:

public class Result
{
// Properties ---------------------------------------------------------
public bool Success { get; set; }
public string Error { get; set; }
}

public class Functions
{
// This function will be triggered based on the schedule you have set for this WebJob
// This function will enqueue a message on an Azure Queue called queue
[NoAutomaticTrigger]
public async static void ManualTrigger(TextWriter log, int value)
{
using (var client = new HttpClient())
{
var rootUrl = ConfigurationManager.AppSettings.Get("WebJobTargetUrl");
client.BaseAddress = new System.Uri(rootUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Console.WriteLine("Call service");
var response = await client.PostAsJsonAsync("api/Reminder/ProcessDueReminders", new { ItemID = 1 });
Console.WriteLine("After service");
var result = await response.Content.ReadAsAsync<Result>();
Console.WriteLine("After result");

if (result.Success)
Console.WriteLine("Reminders Processed");
else
Console.WriteLine("Reminder process error: " + result.Error);
}
}
}

来自门户的执行日志是: Execution Logs

我相信这与异步操作有关,但我无法找出可行的模式。任何帮助将不胜感激。

最佳答案

您必须将自己的异步方法的返回值定义为 Task 而不是 void

在相关说明中,您应该在方法名称后加上 Async 后缀。这并不能解决问题,但它表明您正在使用异步/等待模式。

关于azure - 为什么 HttpClient PostAsJsonAsync 退出 Azure Web 作业而不运行其后的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35661121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com