Mar*_*sen 5 c# asynchronous asp.net-web-api2
我正在开发一个从客户端获取数据的web-api,并将其保存以供以后使用.现在我有一个需要知道所有事件的外部系统,所以我想在我的web-api中设置一个通知组件.
我所做的是,在保存数据后,我SendNotification(message)
在我的新组件中执行一个方法.同时我不希望我的客户等待甚至知道我们正在发送通知,所以我想201 Created / 200 OK
尽快向我的客户返回响应.
是的,这是一个即发即忘的场景.我希望通知组件处理所有异常情况(如果通知失败,api的客户端根本不关心).
我尝试过使用async/await
,但这在web-api中不起作用,因为当请求线程终止时,异步操作也会这样做.
所以我看了看Task.Run()
.
我的控制器看起来像这样:
public IHttpActionResult PostData([FromBody] Data data) {
_dataService.saveData(data);
//This could fail, and retry strategy takes time.
Task.Run(() => _notificationHandler.SendNotification(new Message(data)));
return CreatedAtRoute<object>(...);
}
Run Code Online (Sandbox Code Playgroud)
和我的NotificationHandler中的方法
public void SendNotification(Message message) {
//..send stuff to a notification server somewhere, syncronously.
}
Run Code Online (Sandbox Code Playgroud)
我在C#世界中相对较新,我不知道是否有更优雅(或正确)的方式.使用这种方法有任何陷阱吗?