Как сериализовать Stream используя Newtonsoft json?
У меня есть две структуры данных
public class Document
{
private readonly ICollection<Image> _images = new List<Image>();
public string Token { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public DateTime DateDownload { get; set; }
public string Url { get; set; }
public string File { get; set; }
public string HtmlMarkup { get; set; }
public string Category { get; set; }
public string Author { get; set; }
public List<string> Titles { get; set; }
public List<string> Text { get; set; }
public List<string> TableContent { get; set; }
public List<Image> Images => _images.ToList();
public void AddImage(Image image)
{
_images.Add(image);
}
}
public class Image
{
public Stream FileStream { get; set; }
public string FileName { get; set; }
}
Я ставлю документ в очередь используя RabbitMq, при сериализации Image.FileStream я получаю исключение, в котором говорится, что не удаётся сериализовать свойство с типом Stream
public class RabbitMqReceiver : IReceiver
{
private readonly IConnection _connection;
private readonly IModel _channel;
private readonly string _queue;
public RabbitMqReceiver(string uri, string queue)
{
if (string.IsNullOrWhiteSpace(uri))
throw new ArgumentNullException(nameof(uri));
if (string.IsNullOrWhiteSpace(queue))
throw new ArgumentNullException(nameof(queue));
_queue = queue;
IConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.Uri = new Uri(uri);
_connection = connectionFactory.CreateConnection();
_channel = _connection.CreateModel();
_channel.QueueDeclare(
queue: _queue,
durable: true,
exclusive: false,
autoDelete: false);
_channel.BasicQos(0, 1, false);
}
public event ReceivedEventHandler? Received;
public void Start()
{
var consumer = new EventingBasicConsumer(_channel);
consumer.Received += (model, eventArgs) =>
{
byte[] message = eventArgs.Body.ToArray();
string json = Encoding.UTF8.GetString(message);
var document = JsonConvert.DeserializeObject<Document>(json);
Received?.Invoke(document, eventArgs.DeliveryTag);
};
_channel.BasicConsume(
queue: _queue,
consumer: consumer);
}
public void Ack(ulong deliveryTag)
{
_channel?.BasicAck(
deliveryTag: deliveryTag,
multiple: false);
}
public void Requeue(Document document)
{
IBasicProperties properties = _channel.CreateBasicProperties();
properties.Persistent = true;
string json = JsonConvert.SerializeObject(document);
byte[] message = Encoding.UTF8.GetBytes(json);
_channel.BasicPublish(
exchange: string.Empty,
routingKey: _queue,
basicProperties: properties,
body: message);
}
public void Dispose()
{
_channel?.Dispose();
_connection?.Dispose();
}
}
public class RabbitMqSender : ISender
{
private readonly object _lockObject = new object();
private readonly IConnection _connection;
private readonly string _queue;
private IModel _channel;
public RabbitMqSender(string uri, string queue)
{
if (string.IsNullOrWhiteSpace(uri))
throw new ArgumentNullException(nameof(uri));
if (string.IsNullOrWhiteSpace(queue))
throw new ArgumentNullException(nameof(queue));
_queue = queue;
IConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.Uri = new Uri(uri);
_connection = connectionFactory.CreateConnection();
}
public void Send(Document document)
{
EnsureChannelIsPresent();
IBasicProperties properties = _channel.CreateBasicProperties();
properties.Persistent = true;
string json = JsonConvert.SerializeObject(document);
byte[] message = Encoding.UTF8.GetBytes(json);
_channel.BasicPublish(
exchange: string.Empty,
routingKey: _queue,
basicProperties: properties,
body: message);
}
public void Dispose()
{
_channel?.Dispose();
_connection?.Dispose();
}
private void EnsureChannelIsPresent()
{
lock (_lockObject)
{
if (_channel == null)
CreateChannel();
}
}
private void CreateChannel()
{
_channel = _connection.CreateModel();
_channel.QueueDeclare(
queue: _queue,
durable: true,
exclusive: false,
autoDelete: false);
_channel.BasicQos(0, 1, false);
}
}