gpt4 book ai didi

c# - 这种一劳永逸的方法是否正确?

转载 作者:行者123 更新时间:2023-11-30 17:03:00 24 4
gpt4 key购买 nike

我已经实现了 instagram api realtime updates .基本上,当根据我的订阅添加了新图像时,他们会向我提供的 url 发出 POST 请求。

他们说:

“您应该在 2 秒超时内确认 POST——如果您需要对接收到的信息进行更多处理,您可以在异步任务中进行。”

所以我构建了类似的东西:

    [HttpPost]
[ActionName("realtime")]
public async Task<ActionResult> IndexPost()
{
var form = Request.Form;

Request.InputStream.Position = 0;
System.IO.StreamReader str = new System.IO.StreamReader(Request.InputStream);
string sBuf = str.ReadToEnd();

// deserialize this from json
var serializer = new JavaScriptSerializer();

var updates = serializer.Deserialize<IEnumerable<RealtimeUpdate>>(sBuf).ToList();

ProcessNewTaggedImages(updates);
return new ContentResult { Content = "Ok" };

}

ProcessNewTaggedImages 正在异步运行。

 public async void ProcessNewTaggedImages(List<RealtimeUpdate> updates)
{
Task.Run(() =>
{
// query Instagram api for new images
}
}

所以基本上当 Instagram POST 到 www.mysite.com/realtime 时,它​​不会等待 ProcessNewTaggedImages

我只是想确保这种方法对于即发即弃方法是正确的,因为在 Task.Run 下我收到一条警告:

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the await operator to the result of the call.

但我不想在这里等待,因为

  1. 我的处理结果对 Instagram 调用无关紧要
  2. POST 超时为 2 秒,因此我不想等待此处理。

您能确认我的方向是正确的吗?Ps:POST 工作正常并且一切正常只是想确认我没有做任何 mystake 因为我大多是 C# 中这种异步方法的初学者。

最佳答案

如果您需要即发即弃功能,则无需向您的方法添加异步关键字,因为您没有进行任何等待。因此,从您的代码中删除 async 关键字,编译器就不会提示您的代码。

关于c# - 这种一劳永逸的方法是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19093788/

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