【发布时间】:2019-04-10 00:55:39
【问题描述】:
我遇到了重复数据的问题。 我有一个客户端多次调用 API 来填充一些网格。 API 实例是一种检查数据是否存在的服务,如果不存在则创建所有数据。 问题是当创建数据的方法很重并且需要很长时间时。所以 2 个方法同时运行,并且都保存创建重复的数据 我可以将 API 调用排队并一次执行一个吗? 还是有另一种方法可以实现这一目标? 这里是API的代码
[Produces("application/json")]
[Route("api/v1/Combinations")]
public class CombinationsController : Controller
{
private readonly ICommonService _CommonService;
private readonly ILogger _logger;
public CombinationsController(ICommonService CommonService,
ILogger<CombinationsController> logger)
{
_logger = logger;
_CommonService = CommonService;
}
[HttpPost("GenerateCombinationsByAttributes",Name ="GenerateCombinationsByAttributes")]
public async Task<IActionResult> GenerateCombinationsByAttributes([FromBody] List<Guid> attributeIds)
{
if (attributeIds == null || attributeIds.Count == 0 || attributeIds.Contains(Guid.Empty))
return BadRequest();
var ret = await _CommonService.GenerateCombinationByAttributesList(attributeIds);
return Ok();
}
}
【问题讨论】:
-
把
lock(或MethodImpl(MethodImplOptions.Synchronized)-不确定.net核心是否支持它)放在GenerateCombinationByAttributesList实现中......或在GenerateCombinationByAttributesList中使用事务......这取决于 -
@Selvin 我认为锁是本地的并且仅在微服务实例内部有效,因此每个实例都有自己的锁。微服务的实例是瞬态的
services.AddTransient<ICommonService, CommonService>(); -
好吧,我不知道
services是什么……但我知道的大多数 IoC 都在使用单个服务实例……但即使你仍然可以锁定static object mylock = new object(); -
@Selvin services 是
Microsoft.Extensions.DependencyInjection的IServiceCollection services并且lock内部的async Task也不会导致某种问题?不管怎样,我会试着放一个锁 -
数据库事务呢? (当然,如果您在 GenerateCombinationByAttributesList 中使用 db)
标签: c# entity-framework asp.net-core-2.0 microservices