Пример #1
0
        public ActiveHandlerTrackingEntry(string name, LifetimeTrackingHttpMessageHandler handler, TimeSpan lifetime)
        {
            Name     = name;
            Handler  = handler;
            Lifetime = lifetime;

            _lock = new object();
        }
Пример #2
0
        private ActiveHandlerTrackingEntry CreateHandlerEntry(string name)
        {
            var builder = _iocManager.Resolve <IHttpMessageHandlerBuilder>();

            builder.Name = name ?? throw new ArgumentNullException(nameof(name));

            var filterArray = _filters.ToArray();

            /*
             * HttpClient 配置操作
             *
             * HttpClient 下,在构建HttpMessageHandler时 配置操作
             *
             * HttpClient 下,在构建HttpMessageHandler前,配置操作
             */

            var options = _iocManager.Resolve <HttpClientFactoryOptions>();

            // This is similar to the initialization pattern in:
            // https://github.com/aspnet/Hosting/blob/e892ed8bbdcd25a0dafc1850033398dc57f65fe1/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs#L188
            Action <IHttpMessageHandlerBuilder> configure = (handlerBuilder) => { options.HttpMessageHandlerBuilderActions.ForEach(action => action(handlerBuilder)); };

            for (var i = filterArray.Length - 1; i >= 0; i--)
            {
                configure = filterArray[i].Configure(configure);
            }

            configure(builder);

            // Wrap the handler so we can ensure the inner handler outlives the outer handler.
            var handler = new LifetimeTrackingHttpMessageHandler(builder.Build());

            // Note that we can't start the timer here. That would introduce a very very subtle race condition
            // with very short expiry times. We need to wait until we've actually handed out the handler once
            // to start the timer.
            //
            // Otherwise it would be possible that we start the timer here, immediately expire it (very short
            // timer) and then dispose it without ever creating a client. That would be bad. It's unlikely
            // this would happen, but we want to be sure.
            return(new ActiveHandlerTrackingEntry(name, handler, options.HandlerLifetime));
        }