Example #1
0
        // Internal for tests
        internal ActiveHandlerTrackingEntry CreateHandlerEntry(string name)
        {
            var options = GetOptions(name);
            var builder = new DefaultHttpMessageHandlerBuilder();

            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));

            void Configure(HttpMessageHandlerBuilder b)
            {
                for (var i = 0; i < options.HttpMessageHandlerBuilderActions.Count; i++)
                {
                    options.HttpMessageHandlerBuilderActions[i](b);
                }
            }
        }
        public ActiveHandlerTrackingEntry(
            string name,
            LifetimeTrackingHttpMessageHandler handler,
            TimeSpan lifetime)
        {
            Name     = name;
            Handler  = handler;
            Lifetime = lifetime;

            _lock = new object();
        }