예제 #1
0
        /// <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 GetCaheAsync(context).ConfigureAwait(false);

            if (actionCache != null && actionCache.Value != null)
            {
                context.HttpContext.ResponseMessage = actionCache.Value;
            }
            else
            {
                var client  = context.HttpContext.HttpClient;
                var request = context.HttpContext.RequestMessage;
                using var tokenLinker = new CancellationTokenLinker(context.HttpContext.CancellationTokens);
                var response = await client.SendAsync(request, tokenLinker.Token).ConfigureAwait(false);

                context.HttpContext.ResponseMessage = response;
                await SetCacheAsync(context, actionCache?.Key, response).ConfigureAwait(false);
            }
            return(new ApiResponseContext(context));
        }
예제 #2
0
        /// <summary>
        /// 发送http请求
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="HttpRequestException"></exception>
        /// <returns></returns>
        public static async Task<ApiResponseContext> SendAsync(ApiRequestContext context)
        {
            var actionCache = await GetCaheAsync(context).ConfigureAwait(false);
            if (actionCache != null && actionCache.Value != null)
            {
                context.HttpContext.ResponseMessage = actionCache.Value;
            }
            else
            {
                var client = context.HttpContext.Client;
                using var tokenLinker = new CancellationTokenLinker(context.CancellationTokens);
                var response = await client.SendAsync(context.HttpContext.RequestMessage, tokenLinker.Token).ConfigureAwait(false);

                context.HttpContext.ResponseMessage = response;
                await SetCacheAsync(context, actionCache?.Key, response).ConfigureAwait(false);
            }
            return new ApiResponseContext(context);
        }
예제 #3
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);
            }
        }