/// <summary>
        /// 执行请求
        /// </summary>
        /// <returns></returns>
        private async Task ExecRequestAsync()
        {
            using (var cancellation = this.CreateLinkedTokenSource())
            {
                try
                {
                    var completionOption = this.ApiActionDescriptor.Return.DataType.IsHttpResponseWrapper ?
                                           HttpCompletionOption.ResponseHeadersRead :
                                           HttpCompletionOption.ResponseContentRead;

                    this.ResponseMessage = await this.HttpApiConfig.HttpClient
                                           .SendAsync(this.RequestMessage, completionOption, cancellation.Token)
                                           .ConfigureAwait(false);

                    var result = await this.ApiActionDescriptor.Return.Attribute
                                 .GetTaskResult(this)
                                 .ConfigureAwait(false);

                    ApiValidator.ValidateReturnValue(result, this.HttpApiConfig.UseReturnValuePropertyValidate);
                    this.Result = result;
                }
                catch (Exception ex)
                {
                    this.Exception = ex;
                }
            }
        }
Example #2
0
        /// <summary>
        /// 执行请求
        /// </summary>
        /// <returns></returns>
        private async Task <object> ExecRequestAsync()
        {
            using (var cancellation = this.CreateLinkedTokenSource())
            {
                var cacheAttribute = this.ApiActionDescriptor.Cache;
                var cacheProvider  = this.HttpApiConfig.ResponseCacheProvider;

                var cacheKey    = default(string);
                var cacheResult = ResponseCacheResult.NoValue;
                var cacheEnable = cacheAttribute != null && cacheProvider != null;

                if (cacheEnable == true)
                {
                    cacheKey = await cacheAttribute.GetCacheKeyAsync(this).ConfigureAwait(false);

                    cacheResult = await cacheProvider.GetAsync(cacheKey).ConfigureAwait(false);
                }

                if (cacheResult.HasValue == true)
                {
                    this.ResponseMessage = cacheResult.Value.ToResponseMessage(this.RequestMessage, cacheProvider.Name);
                }
                else
                {
                    var completionOption = this.ApiActionDescriptor.Return.DataType.IsHttpResponseWrapper ?
                                           HttpCompletionOption.ResponseHeadersRead :
                                           HttpCompletionOption.ResponseContentRead;

                    this.ResponseMessage = await this.HttpApiConfig.HttpClient
                                           .SendAsync(this.RequestMessage, completionOption, cancellation.Token)
                                           .ConfigureAwait(false);

                    if (cacheEnable == true)
                    {
                        var cacheEntry = await ResponseCacheEntry.FromResponseMessageAsync(this.ResponseMessage).ConfigureAwait(false);

                        await cacheProvider.SetAsync(cacheKey, cacheEntry, cacheAttribute.Expiration).ConfigureAwait(false);
                    }
                }

                var result = await this.ApiActionDescriptor.Return.Attribute
                             .GetTaskResult(this)
                             .ConfigureAwait(false);

                ApiValidator.ValidateReturnValue(result, this.HttpApiConfig.UseReturnValuePropertyValidate);
                return(result);
            }
        }
        public void ValidateReturnValueTest()
        {
            var value = default(User);

            ApiValidator.ValidateReturnValue(value, true);

            value = new User();
            Assert.Throws <ValidationException>(() => ApiValidator.ValidateReturnValue(value, true));

            value = new User {
                Account = "123"
            };
            ApiValidator.ValidateReturnValue(value, true);

            value = new User {
                Account = "123456"
            };
            Assert.Throws <ValidationException>(() => ApiValidator.ValidateReturnValue(value, true));
        }