Ejemplo n.º 1
0
        async Task <MessageData <TValue> > Put(PipeContext context, Task <TValue> valueTask)
        {
            var repository = _repository;

            if (repository != null || context.TryGetPayload(out repository))
            {
                TimeSpan?timeToLive = default;
                if (context.TryGetPayload(out SendContext sendContext) && sendContext.TimeToLive.HasValue)
                {
                    timeToLive = sendContext.TimeToLive;
                }

                if (timeToLive.HasValue && MessageDataDefaults.ExtraTimeToLive.HasValue)
                {
                    timeToLive += MessageDataDefaults.ExtraTimeToLive;
                }

                if (!timeToLive.HasValue && MessageDataDefaults.TimeToLive.HasValue)
                {
                    timeToLive = MessageDataDefaults.TimeToLive.Value;
                }

                var value = await valueTask.ConfigureAwait(false);

                if (value is string stringValue)
                {
                    MessageData <string> messageData = timeToLive.HasValue
                        ? await _repository.PutString(stringValue, timeToLive.Value, context.CancellationToken).ConfigureAwait(false)
                        : await _repository.PutString(stringValue, context.CancellationToken).ConfigureAwait(false);

                    return((MessageData <TValue>)messageData);
                }

                if (value is byte[] bytesValue)
                {
                    MessageData <byte[]> messageData = timeToLive.HasValue
                        ? await _repository.PutBytes(bytesValue, timeToLive.Value, context.CancellationToken).ConfigureAwait(false)
                        : await _repository.PutBytes(bytesValue, context.CancellationToken).ConfigureAwait(false);

                    return((MessageData <TValue>)messageData);
                }

                if (value is Stream streamValue)
                {
                    MessageData <Stream> messageData = timeToLive.HasValue
                        ? await _repository.PutStream(streamValue, timeToLive.Value, context.CancellationToken).ConfigureAwait(false)
                        : await _repository.PutStream(streamValue, default, context.CancellationToken).ConfigureAwait(false);