Beispiel #1
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);
            }
        }
Beispiel #2
0
        /// <inheritdoc/>
        public override async Task <TResponse> InvokeBindingAsync <TRequest, TResponse>(
            string bindingName,
            string operation,
            TRequest data,
            IReadOnlyDictionary <string, string> metadata = default,
            CancellationToken cancellationToken           = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(bindingName, nameof(bindingName));
            ArgumentVerifier.ThrowIfNullOrEmpty(operation, nameof(operation));

            var bytes    = TypeConverters.ToJsonByteString <TRequest>(data, this.jsonSerializerOptions);
            var response = await MakeInvokeBindingRequestAsync(bindingName, operation, bytes, metadata, cancellationToken);

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