コード例 #1
0
        public async Task <ITaskResult> TryReadResultAsync(ServiceId serviceId, MethodId methodId, string intentId, Type resultValueType, CancellationToken ct)
        {
            var tableName = GetTableQualifiedName(serviceId, methodId);

            var key = new StorageRecord
            {
                service   = serviceId.Name,
                method    = methodId.Name,
                intent_id = intentId
            };

            var query = new StringBuilder("SELECT status, outcome, format, task_result, result, error FROM ")
                        .Append(tableName).Append(" WHERE ");

            WriteValues(query, key, delimiter: " AND ");

            var result = await ExecuteQueryAsync(serviceId, methodId, query.ToString());

            var row = result.FirstOrDefault();

            if (row == null)
            {
                return(null);
            }

            var record = ReadValues(row);

            if (!record.outcome.HasValue)
            {
                return(null);
            }

            if (record.outcome.Value == (int)Outcomes.Canceled)
            {
                return(TaskResult.Create(resultValueType, null, null, isCanceled: true));
            }

            var serializer = !string.IsNullOrEmpty(record.format)
                ? _serializerProvider.GetSerializer(record.format)
                : _serializer;

            var taskResult = TaskResult.CreateEmpty(resultValueType);

            serializer.Populate(record.task_result, (IValueContainer)taskResult);
            return(taskResult);
        }
コード例 #2
0
        public static ITaskResult ToTaskResult(this Task task)
        {
            var status = task.Status;

            if (status != TaskStatus.RanToCompletion && status != TaskStatus.Canceled && status != TaskStatus.Faulted)
            {
                throw new ArgumentException($"The task is not completed and is in '{status}' state.", nameof(task));
            }

            var exception = (task.Exception is AggregateException aggregateException && aggregateException.InnerExceptions?.Count == 1)
                ? aggregateException.InnerException
                : task.Exception;

            var valueType = task.GetResultType();

            if (valueType == null ||
                valueType == typeof(void) ||
                valueType == TaskAccessor.VoidTaskResultType ||
                valueType == typeof(object))
            {
                return(new TaskResult
                {
                    Value = status == TaskStatus.RanToCompletion ? task.GetResult() : null,
                    Exception = status == TaskStatus.Faulted ? exception : null,
                    IsCanceled = status == TaskStatus.Canceled
                });
            }
            else
            {
                return(TaskResult.Create(
                           valueType,
                           status == TaskStatus.RanToCompletion ? task.GetResult() : null,
                           status == TaskStatus.Faulted ? exception : null,
                           status == TaskStatus.Canceled));
            }
        }