コード例 #1
0
        async Task IHttpHandler.OnSending(HttpSendingContext context)
        {
            Settings.ResultInspector = cacheEntry =>
            {
                ((HttpResponseMessage)cacheEntry.Value).RequestMessage = context.Request;
            };

            await TryGetFromCache(context);
        }
コード例 #2
0
        private async Task <HttpResponseMessage> ResultFromRequestAsync(IHttpBuilderContext context, HttpRequestMessage request, CancellationToken token)
        {
            ExceptionDispatchInfo capturedException = null;
            HttpResponseMessage   response          = null;

            try
            {
                var sendingContext = new HttpSendingContext(context, request);

                token.ThrowIfCancellationRequested();

                await context.HandlerRegister.OnSending(sendingContext);

                if (sendingContext.Result != null)
                {
                    response = sendingContext.Result;
                }
                else
                {
                    token.ThrowIfCancellationRequested();

                    using (var client = _clientBuilder.Build())
                        response = await client.SendAsync(request, context.CompletionOption, token);
                }

                if (!context.IsSuccessfulResponse(response) && context.ExceptionFactory != null)
                {
                    var ex = context.ExceptionFactory(response, request);

                    if (ex != null)
                    {
                        throw ex;
                    }
                }

                var sentContext = new HttpSentContext(context, request, response);

                token.ThrowIfCancellationRequested();

                await context.HandlerRegister.OnSent(sentContext);

                response = sentContext.Result;
            }
            catch (Exception ex)
            {
                capturedException = ExceptionDispatchInfo.Capture(ex);
            }

            if (capturedException != null)
            {
                var exContext = new HttpExceptionContext(context, response, capturedException.SourceException);

                await context.HandlerRegister.OnException(exContext);

                if (!exContext.ExceptionHandled)
                {
                    capturedException.Throw();
                }
            }

            return(response);
        }
コード例 #3
0
        public override Task OnSending(HttpSendingContext context)
        {
            context.Items["CustomHttpHandler"] = "CustomHttpHandler: It Works!";

            return(base.OnSending(context));
        }