Beispiel #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();
                }
            }
        }
        public Task Dispatch(TransportOperations outgoingMessages, TransportTransaction transaction, ContextBag context)
        {
            foreach (var operation in outgoingMessages.UnicastTransportOperations)
            {
                var destinationBasePath = DirectoryBuilder.BuildBasePath(operation.Destination);
                var nativeMessageId     = Guid.NewGuid().ToString();
                var bodyPath            = Path.Combine(destinationBasePath, ".bodies", $"{nativeMessageId}.xml");

                var bodyDirectory = Path.GetDirectoryName(bodyPath);
                if (bodyDirectory != null)
                {
                    if (!Directory.Exists(bodyDirectory))
                    {
                        Directory.CreateDirectory(bodyDirectory);
                    }
                }

                File.WriteAllBytes(bodyPath, operation.Message.Body);

                var messageContents = new List <string>
                {
                    bodyPath,
                    HeaderSerializer.Serialize(operation.Message.Headers)
                };

                var messagePath = Path.Combine(destinationBasePath, $"{nativeMessageId}.txt");

                // Write to a temp file first so an atomic move can be done.
                // This avoids the file being locked when the receiver triest to process it.
                var tempFilePath = Path.GetTempFileName();
                File.WriteAllLines(tempFilePath, messageContents);
                File.Move(tempFilePath, messagePath);
            }

            return(Task.CompletedTask);
        }