/// <summary>
        /// 发送http请求
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="HttpRequestException"></exception>
        /// <exception cref="ApiInvalidConfigException"></exception>
        /// <returns></returns>
        public static async Task <ApiResponseContext> SendAsync(ApiRequestContext context)
        {
            if (context.HttpContext.RequestMessage.RequestUri == null)
            {
                throw new ApiInvalidConfigException(Resx.required_HttpHost);
            }

            var actionCache = await context.GetCaheAsync().ConfigureAwait(false);

            if (actionCache != null && actionCache.Value != null)
            {
                context.HttpContext.ResponseMessage = actionCache.Value;
            }
            else
            {
                var client           = context.HttpContext.HttpClient;
                var request          = context.HttpContext.RequestMessage;
                var completionOption = context.GetCompletionOption();

                using var tokenLinker = new CancellationTokenLinker(context.HttpContext.CancellationTokens);
                var response = await client.SendAsync(request, completionOption, tokenLinker.Token).ConfigureAwait(false);

                context.HttpContext.ResponseMessage = response;
                await context.SetCacheAsync(actionCache?.Key, response).ConfigureAwait(false);
            }
            return(new ApiResponseContext(context));
        }
Example #2
0
        /// <summary>
        /// 发送http请求
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="HttpRequestException"></exception>
        /// <returns></returns>
        private static async Task SendCoreAsync(ApiRequestContext context)
        {
            var actionCache = await context.GetCaheAsync().ConfigureAwait(false);

            if (actionCache != null && actionCache.Value != null)
            {
                context.HttpContext.ResponseMessage = actionCache.Value;
            }
            else
            {
                var client           = context.HttpContext.HttpClient;
                var request          = context.HttpContext.RequestMessage;
                var completionOption = context.GetCompletionOption();

                using var tokenLinker = new CancellationTokenLinker(context.HttpContext.CancellationTokens);
                var response = await client.SendAsync(request, completionOption, tokenLinker.Token).ConfigureAwait(false);

                context.HttpContext.ResponseMessage = response;
                await context.SetCacheAsync(actionCache?.Key, response).ConfigureAwait(false);
            }
        }