コード例 #1
0
        private async Task ProcessFileWithTransaction(string filePath, string messageId)
        {
            using (var transaction = new DirectoryBasedTransaction(_messageDirectory))
            {
                transaction.BeginTransaction(filePath);

                var messageFile       = File.ReadAllLines(transaction.FileToProcess);
                var bodyPath          = messageFile.First();
                var messageHeaderJson = string.Join("", messageFile.Skip(1));
                var messageHeaders    = HeaderSerializer.DeSerialize(messageHeaderJson);

                if (RemoveFileIfExpired(messageHeaders, transaction))
                {
                    return;
                }

                var fileContents         = File.ReadAllBytes(bodyPath);
                var transportTransaction = new TransportTransaction();
                transportTransaction.Set(transaction);

                var shouldCommit = await HandleMessageWithRetries(messageId, messageHeaders, fileContents, transportTransaction, 1);

                if (shouldCommit)
                {
                    transaction.Commit();
                }
            }
        }
コード例 #2
0
        private static bool RemoveFileIfExpired(Dictionary <string, string> messageHeaders, DirectoryBasedTransaction transaction)
        {
            string timeToBeReturnedString;

            if (messageHeaders.TryGetValue(Headers.TimeToBeReceived, out timeToBeReturnedString))
            {
                // This works because moving the file inside the transaction preserves create time
                var sentTime         = File.GetCreationTimeUtc(transaction.FileToProcess);
                var timeToBeReturned = TimeSpan.Parse(timeToBeReturnedString);
                if (sentTime + timeToBeReturned < DateTime.UtcNow)
                {
                    return(true);
                }
            }
            return(false);
        }