【问题标题】:Run code after returning data返回数据后运行代码
【发布时间】:2017-09-11 13:23:37
【问题描述】:

我有一个这样的 web api:

public async Task<IHttpActionResult> PostTest(Model model)
{
    db.UserOrders.Add(model);
    await db.SaveChangesAsync();
    await PushUtils.SendPush("title" , "message"); // heavy task
    return Ok();
}


在这种方法中,用户必须等到繁重的任务执行完毕。
有没有什么办法可以在返回值给用户后执行这个任务?

【问题讨论】:

标签: asp.net-mvc asp.net-web-api


【解决方案1】:

我用过Hangfire

public async Task<IHttpActionResult> PostTest(Model model)
{
    db.UserOrders.Add(model);
    await db.SaveChangesAsync();

    BackgroundJob.Schedule(
    () => PushUtils.SendPush("title" , "message"),
    TimeSpan.FromSeconds(7));

    return Ok();
}

【讨论】:

    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2019-11-19
    • 2015-08-13
    相关资源
    最近更新 更多