private CsvFileEnumerable <TRecord> ExportAndGetRecordsFromList <TRecord>(int listId, string downloadDir, ExportContext context, Func <int, string> createFileName, TimeSpan?exportTimeout, CancellationToken cancellationToken) where TRecord : CsvRecord { int?exportId = null; var exportTimeoutNotNull = exportTimeout ?? TimeSpan.FromMinutes(5); try { var export = _exportService.Create(listId, context, cancellationToken); ConnectClient.Logger.Trace($"ExportReader-ExportAndGetRecordsFromList: Export {export.Id} created"); exportId = export.Id; var isReady = WaitForExportToFinish(exportId.Value, exportTimeoutNotNull, cancellationToken); if (!isReady) { ConnectClient.Logger.Trace($"ExportReader-ExportAndGetRecordsFromList: Export {exportId} not ready in time"); throw new ExportIsNotCompleteException(exportId.Value); } ConnectClient.Logger.Trace($"ExportReader-ExportAndGetRecordsFromList: Export {exportId} ready"); return(GetExportedRecords <TRecord>(exportId.Value, downloadDir, createFileName?.Invoke(exportId.Value), cancellationToken)); } finally { if (exportId.HasValue) { _exportService.Delete(exportId.Value, false, cancellationToken); } } }