Exemplo n.º 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);
Exemplo n.º 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);
            }
        }
Exemplo n.º 3
0
        public override ValueTask <TValue> GetStateAsync <TValue>(string storeName, string key, ConsistencyMode?consistencyMode = default, CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName));
            ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key));

            if (this.State.TryGetValue(key, out var obj))
            {
                return(new ValueTask <TValue>((TValue)obj));
            }
            else
            {
                return(new ValueTask <TValue>(default(TValue)));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets a <see cref="StateEntry{T}" /> for the current value associated with the <paramref name="key" /> from
        /// the Dapr state store.
        /// </summary>
        /// <param name="storeName">The name of the state store.</param>
        /// <param name="key">The state key.</param>
        /// <param name="consistencyMode">The consistency mode <see cref="ConsistencyMode" />.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
        /// <typeparam name="TValue">The data type of the value to read.</typeparam>
        /// <returns>A <see cref="ValueTask" /> that will return the <see cref="StateEntry{T}" /> when the operation has completed.</returns>
        public async ValueTask <StateEntry <TValue> > GetStateEntryAsync <TValue>(string storeName, string key, ConsistencyMode?consistencyMode = default, CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName));
            ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key));

            var(state, etag) = await this.GetStateAndETagAsync <TValue>(storeName, key, consistencyMode, cancellationToken);

            return(new StateEntry <TValue>(this, storeName, key, state, etag));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Gets the current value associated with the <paramref name="key" /> from the Dapr state store.
 /// </summary>
 /// <param name="storeName">The name of state store to read from.</param>
 /// <param name="key">The state key.</param>
 /// <param name="consistencyMode">The consistency mode <see cref="ConsistencyMode" />.</param>
 /// <param name="metadata">An key/value pair that may be consumed by the state store.  This is dependent on the type of state store used.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
 /// <typeparam name="TValue">The data type of the value to read.</typeparam>
 /// <returns>A <see cref="ValueTask{T}" /> that will return the value when the operation has completed.</returns>
 public abstract ValueTask <TValue> GetStateAsync <TValue>(string storeName, string key, ConsistencyMode?consistencyMode = default, Dictionary <string, string> metadata = default, CancellationToken cancellationToken = default);
Exemplo n.º 6
0
        public override 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));

            if (this.State.TryGetValue(key, out var obj))
            {
                var b = obj as byte[];
                if (b != null)
                {
                    return(Task.FromResult(JsonSerializer.Deserialize <TValue>(b)));
                }

                return(Task.FromResult((TValue)obj));
            }
            else
            {
                return(Task.FromResult(default(TValue)));
            }
        }