private async Task ProcessInIsolation(Stats statFile, Stream stream, string fileName, IPAddress remoteIp) { var fileNumber = GetFileNumber(fileName); // We only want to process one file at a time await SemaphoreSlim.WaitAsync(); try { using (var trans = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await _processor.Process(statFile, fileNumber); var path = Path.Combine($"{statFile.Date:yyyy-MM-dd} {statFile.Type}{(statFile.SubType != null ? " " + statFile.SubType : string.Empty)}", fileName); await _db.Insert(new DSJTournaments.Data.Schema.Upload { Path = Path.Combine(_fileArchive.BasePath, path), RemoteIp = remoteIp, UploadedAt = DateTime.Now }); await _fileArchive.ArchiveFile(stream, path); if (statFile is StandingStats || statFile is TeamFinalResultStats) { await _db.ExecuteAsync("REFRESH MATERIALIZED VIEW jumper_results"); } trans.Complete(); } } finally { SemaphoreSlim.Release(); } }