Add new Hcs.Broker to communicate with ГИС ЖКХ via CryptoPro LibCore
This commit is contained in:
43
Hcs.Broker/MessageCapturer/FileMessageCapturer.cs
Normal file
43
Hcs.Broker/MessageCapturer/FileMessageCapturer.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using Hcs.Broker.Logger;
|
||||
using System.Text;
|
||||
|
||||
namespace Hcs.Broker.MessageCapturer
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация механизма захвата содержимого сообщений SOAP, записывающая
|
||||
/// каждое сообщение в отдельный файл на диске
|
||||
/// </summary>
|
||||
public class FileMessageCapturer(string directory, ILogger logger) : IMessageCapturer
|
||||
{
|
||||
private readonly string directory = directory;
|
||||
private readonly ILogger logger = logger;
|
||||
|
||||
public void CaptureMessage(bool sent, string messageBody)
|
||||
{
|
||||
var index = 0;
|
||||
var maxIndex = 1000000;
|
||||
string fileName;
|
||||
do
|
||||
{
|
||||
index += 1;
|
||||
|
||||
if (index > maxIndex)
|
||||
{
|
||||
throw new Exception("index value exceeds maxIndex value");
|
||||
}
|
||||
|
||||
fileName = index.ToString("D3") + "_" + (sent ? "message" : "response") + ".xml";
|
||||
|
||||
if (!string.IsNullOrEmpty(directory))
|
||||
{
|
||||
fileName = Path.Combine(directory, fileName);
|
||||
}
|
||||
}
|
||||
while (File.Exists(fileName));
|
||||
|
||||
logger?.WriteLine($"Capturing message to file {fileName}...");
|
||||
|
||||
File.WriteAllText(fileName, messageBody, Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user