/
vshmidt
/
masstransit
Обзор
Документация
Войти
/
vshmidt
/
masstransit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/MassTransit/Serialization/SystemTextJsonRawMessageBody.cs
79 строк
2 KB
Chris Patterson
Integration of all external packages into MassTransit, split out into Abstractions, Middleware, and the core MassTransit assembly.
22 янв 2022, 18:24
22 янв 2022, 18:24
b5b4f50
Код
Авторство
О чём код?
#nullable enable namespace MassTransit.Serialization { using System; using System.IO; using System.Runtime.Serialization; using System.Text; using System.Text.Json; public class SystemTextJsonRawMessageBody<TMessage> : MessageBody where TMessage : class { readonly object? _message; readonly JsonSerializerOptions _options; byte[]? _bytes; string? _string; public SystemTextJsonRawMessageBody(SendContext<TMessage> context, JsonSerializerOptions options, object? message = null) { _options = options; _message = message ?? context.Message; } public long? Length => _bytes?.Length ?? _string?.Length; public Stream GetStream() { return new MemoryStream(GetBytes(), false); } public byte[] GetBytes() { if (_bytes != null) return _bytes; if (_string != null) { _bytes = Encoding.UTF8.GetBytes(_string); return _bytes; } try { _bytes = JsonSerializer.SerializeToUtf8Bytes(_message, _options); return _bytes; } catch (Exception ex) { throw new SerializationException("Failed to serialize message", ex); } } public string GetString() { if (_string != null) return _string; if (_bytes != null) { _string = Encoding.UTF8.GetString(_bytes); return _string; } try { _string = JsonSerializer.Serialize(_message, _options); return _string; } catch (Exception ex) { throw new SerializationException("Failed to serialize message", ex); } } } }