<commit_before>﻿using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.Context;
using Castle.MicroKernel.Lifestyle;

namespace Castle.Windsor.MsDependencyInjection
{
    /// <summary>
    /// Extends Windsor's <see cref="ScopedLifestyleManager"/> to work as 
    /// MS style scope using <see cref="MsScopedAccesor"/>.
    /// </summary>
    public class MsScopedLifestyleManager : ScopedLifestyleManager
    {
        private GlobalMsLifetimeScopeProvider _globalMsLifetimeScopeProvider;

        public MsScopedLifestyleManager()
            : base(new MsScopedAccesor())
        {

        }

        public override void Init(IComponentActivator componentActivator, IKernel kernel, ComponentModel model)
        {
            _globalMsLifetimeScopeProvider = kernel.Resolve<GlobalMsLifetimeScopeProvider>();

            base.Init(componentActivator, kernel, model);
        }

        public override object Resolve(CreationContext context, IReleasePolicy releasePolicy)
        {
            if (MsLifetimeScope.Current == null)
            {
                using (MsLifetimeScope.Using(_globalMsLifetimeScopeProvider.LifetimeScope))
                {
                    return base.Resolve(context, releasePolicy);
                }
            }

            return base.Resolve(context, releasePolicy);
        }
    }
}<commit_msg>Track object if it's not already being tracked.<commit_after>﻿using Castle.MicroKernel;
using Castle.MicroKernel.Context;
using Castle.MicroKernel.Lifestyle;

namespace Castle.Windsor.MsDependencyInjection
{
    /// <summary>
    /// Extends Windsor's <see cref="ScopedLifestyleManager"/> to work as 
    /// MS style scope using <see cref="MsScopedAccesor"/>.
    /// </summary>
    public class MsScopedLifestyleManager : ScopedLifestyleManager
    {
        public MsScopedLifestyleManager()
            : base(new MsScopedAccesor())
        {

        }

        public override object Resolve(CreationContext context, IReleasePolicy releasePolicy)
        {
            if (MsLifetimeScope.Current == null)
            {
                //Act as transient!
                var burden = CreateInstance(context, false);
                if (!releasePolicy.HasTrack(burden.Instance))
                {
                    Track(burden, releasePolicy);
                }

                return burden.Instance;
            }

            return base.Resolve(context, releasePolicy);
        }
    }
}
{'fraction_non_alphanumeric': 0.08621390809272061, 'fraction_numerical': 0.0, 'mean_word_length': 2.2325886990801576, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2893307', 'n_tokens_mistral': 657, 'n_tokens_neox': 610, 'n_words': 132}