Пример #1
0
        /// <inheritdoc/>
        public override async ValueTask <TValue> GetStateAsync <TValue>(
            string storeName,
            string key,
            ConsistencyMode?consistencyMode      = default,
            Dictionary <string, string> metadata = default,
            CancellationToken cancellationToken  = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName));
            ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key));

            var getStateEnvelope = new Autogenerated.GetStateRequest()
            {
                StoreName = storeName,
                Key       = key,
            };

            if (metadata != null)
            {
                getStateEnvelope.Metadata.Add(metadata);
            }

            if (consistencyMode != null)
            {
                getStateEnvelope.Consistency = GetStateConsistencyForConsistencyMode(consistencyMode.Value);
            }

            var response = await this.MakeGrpcCallHandleError(
                options => client.GetStateAsync(getStateEnvelope, options),
                cancellationToken);

            if (response.Data.IsEmpty)
            {
                return(default);
Пример #2
0
        /// <inheritdoc/>
        public override async Task <TValue> GetStateAsync <TValue>(
            string storeName,
            string key,
            ConsistencyMode?consistencyMode = default,
            IReadOnlyDictionary <string, string> metadata = default,
            CancellationToken cancellationToken           = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName));
            ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key));

            var envelope = new Autogenerated.GetStateRequest()
            {
                StoreName = storeName,
                Key       = key,
            };

            if (metadata != null)
            {
                foreach (var kvp in metadata)
                {
                    envelope.Metadata.Add(kvp.Key, kvp.Value);
                }
            }

            if (consistencyMode != null)
            {
                envelope.Consistency = GetStateConsistencyForConsistencyMode(consistencyMode.Value);
            }

            var options = CreateCallOptions(headers: null, cancellationToken);

            Autogenerated.GetStateResponse response;

            try
            {
                response = await client.GetStateAsync(envelope, options);
            }
            catch (RpcException ex)
            {
                throw new DaprException("State operation failed: the Dapr endpoint indicated a failure. See InnerException for details.", ex);
            }

            try
            {
                return(TypeConverters.FromJsonByteString <TValue>(response.Data, this.JsonSerializerOptions));
            }
            catch (JsonException ex)
            {
                throw new DaprException("State operation failed: the state payload could not be deserialized. See InnerException for details.", ex);
            }
        }