From 6ee0fc5a139d65f5d61fab7ccab61bf0a4aeab2f Mon Sep 17 00:00:00 2001 From: Andris Rubenis Date: Wed, 18 Dec 2019 10:20:06 +0000 Subject: [PATCH] Don't read files which are already openned --- Common/Helpers/FileOperationsHelper.cs | 20 +++++++++++++------- Job.Upload/Upload.cs | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Common/Helpers/FileOperationsHelper.cs b/Common/Helpers/FileOperationsHelper.cs index 80fbadf..a71e5c7 100644 --- a/Common/Helpers/FileOperationsHelper.cs +++ b/Common/Helpers/FileOperationsHelper.cs @@ -34,16 +34,22 @@ namespace RecurringIntegrationsScheduler.Common.Helpers /// /// File path /// Stream - public static Stream Read(string filePath) + public static Stream Read(string filePath, FileShare fileShare = FileShare.ReadWrite) { if (File.Exists(filePath)) { - return new FileStream(filePath, - FileMode.Open, - FileAccess.ReadWrite, - FileShare.ReadWrite, - 4096, - true); + try + { + return new FileStream(filePath, + FileMode.Open, + FileAccess.ReadWrite, + fileShare, + 4096, + true); + } + catch (IOException) + { + } } return null; } diff --git a/Job.Upload/Upload.cs b/Job.Upload/Upload.cs index 23a79e6..24bc674 100644 --- a/Job.Upload/Upload.cs +++ b/Job.Upload/Upload.cs @@ -194,7 +194,7 @@ namespace RecurringIntegrationsScheduler.Job } fileCount++; - var sourceStream = _retryPolicyForIo.Execute(() => FileOperationsHelper.Read(dataMessage.FullPath)); + var sourceStream = _retryPolicyForIo.Execute(() => FileOperationsHelper.Read(dataMessage.FullPath, FileShare.None)); if (sourceStream == null) continue;//Nothing to do here sourceStream.Seek(0, SeekOrigin.Begin);