示例#1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (this.Interceptor == null)
            {
                this.Interceptor = this.Services.GetService <HttpClientInterceptor>();
            }
            var response  = default(HttpResponseMessage);
            var exception = default(Exception);
            var args      = new HttpClientInterceptorEventArgs(request);

            try
            {
                await this.Interceptor.InvokeBeforeSendAsync(args);

                if (args.Cancel)
                {
                    response = new HttpResponseMessage(HttpStatusCode.NoContent);
                }
                else
                {
                    response = await(SendAsyncMethod.Invoke(this.BaseHandler, new object[] { request, cancellationToken }) as Task <HttpResponseMessage>);
                }
                return(response);
            }
            catch (Exception e)
            {
                exception = e;
                throw;
            }
            finally
            {
                if (!args.Cancel)
                {
                    var argsAfter = new HttpClientInterceptorEventArgs(request, response, exception);
                    await this.Interceptor.InvokeAfterSendAsync(argsAfter);

                    await args._AsyncTask;
                }
            }
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (this.Interceptor == null)
            {
                this.Interceptor = this.Services.GetService <HttpClientInterceptor>();
            }
            var response = default(HttpResponseMessage);

            try
            {
                await this.Interceptor.InvokeBeforeSendAsync(new HttpClientInterceptorEventArgs(request, response));

                response = await(SendAsyncMethod.Invoke(this.BaseHandler, new object[] { request, cancellationToken }) as Task <HttpResponseMessage>);
                return(response);
            }
            finally
            {
                var args = new HttpClientInterceptorEventArgs(request, response);
                await this.Interceptor.InvokeAfterSendAsync(args);

                await args._AsyncTask;
            }
        }
        private async Task InvokeAsync(HttpClientInterceptorEventHandler asyncEventHandler, HttpClientInterceptorEventArgs args)
        {
            if (asyncEventHandler == null)
            {
                return;
            }

            var asyncHandlerTasks = asyncEventHandler.GetInvocationList()
                                    .Cast <HttpClientInterceptorEventHandler>()
                                    .Select(handler => handler.Invoke(this, args))
                                    .ToArray();
            await Task.WhenAll(asyncHandlerTasks);
        }
 internal async Task InvokeAfterSendAsync(HttpClientInterceptorEventArgs args)
 {
     this.AfterSend?.Invoke(this, args);
     await InvokeAsync(this.AfterSendAsync, args);
 }
 internal void InvokeAfterSend(HttpClientInterceptorEventArgs args)
 {
     this.AfterSend?.Invoke(this, args);
 }
 internal void InvokeBeforeSend(HttpClientInterceptorEventArgs args)
 {
     this.BeforeSend?.Invoke(this, args);
 }