【发布时间】:2020-04-04 15:33:42
【问题描述】:
我有一个 .net core 3.1 webapi 项目。在某些控制器中,我需要执行一些不会影响对客户端的响应的代码。
特别是,我有一个方法可以返回带有个人资料信息的 json,当用户访问该个人资料的页面时会调用该方法。获取配置文件信息后,我需要记录用户访问此页面,但为了更快的响应,我想在此日志操作之前返回响应。
[Route("[controller]")]
[ApiController]
public class ProfilesController : ControllerBase
{
[HttpGet("{id}")]
public async Task<ActionResult<Profile>> GetProfileById([FromRoute] int id)
{
Profile profile = await _profileService.GetByIDAsync(id);
// DO THIS OPERATION IN BACKGROUND AND DON'T WAIT TO RETURN RESPONSE
await _logService.LogProfileVisit(id);
return Ok(profile);
}
}
我该怎么做?
谢谢。
【问题讨论】:
-
只需删除
await,您不再等待回复。 -
如果
_logService是Scoped,您不能只删除await。因为它将在请求处理结束时被销毁。
标签: c# .net-core asp.net-core-webapi