Esempio n. 1
0
        private object ConvertToMessagePayload(PayloadDescriptor descriptor)
        {
            // NOTE: due to a bug in system.text.json an additional conversion
            // is made to have proper dictionary key casing - on envelope keys only though!
            // For reference: https://github.com/dotnet/runtime/issues/31849

            string MakeKeyFrom(string key) => _jsonSerializerOptions.DictionaryKeyPolicy.ConvertName(key);

            var envelope = new Dictionary <string, object>
            {
                {
                    MakeKeyFrom("MessageId"),
                    descriptor.MessageId
                },
                {
                    MakeKeyFrom("Type"),
                    descriptor.MessageType
                }
            };

            var messageHeaders = descriptor.MessageHeaders
                                 .Where(k => !string.Equals(k.Key, "messageId", StringComparison.InvariantCultureIgnoreCase))
                                 .Where(k => !string.Equals(k.Key, "type", StringComparison.InvariantCultureIgnoreCase));

            foreach (var(key, value) in messageHeaders)
            {
                envelope.Add(MakeKeyFrom(key), value);
            }

            envelope.Add(MakeKeyFrom("Data"), descriptor.MessageData);

            return(envelope);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public Task <string> Serialize(PayloadDescriptor payloadDescriptor)
        {
            var result = JsonSerializer.Serialize(
                value: ConvertToMessagePayload(payloadDescriptor),
                options: _jsonSerializerOptions
                );

            return(Task.FromResult(result));
        }
        private object ConvertToMessagePayload(PayloadDescriptor descriptor)
        {
            // NOTE: due to a bug in system.text.json an additional conversion
            // is made to have proper dictionary key casing - on envelope keys only though!
            // For reference: https://github.com/dotnet/runtime/issues/31849

            string MakeKeyFrom(string key) => _jsonSerializerOptions.DictionaryKeyPolicy.ConvertName(key);

            var envelope = new Dictionary <string, object>();

            envelope.Add(MakeKeyFrom("MessageId"), descriptor.MessageId);
            envelope.Add(MakeKeyFrom("Type"), descriptor.MessageType);

            foreach (var(key, value) in descriptor.MessageHeaders)
            {
                envelope.Add(MakeKeyFrom(key), value);
            }

            envelope.Add(MakeKeyFrom("Data"), descriptor.MessageData);

            return(envelope);
        }