<commit_before>using System;
using Microsoft.Extensions.DependencyInjection;

namespace DotVVM.Framework.Configuration
{
    public class ServiceLocator
    {
        private IServiceCollection serviceCollection;
        private IServiceProvider serviceProvider;

        public ServiceLocator(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            this.serviceProvider = serviceProvider;
        }

        public ServiceLocator(IServiceCollection serviceCollection)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            this.serviceCollection = serviceCollection;
        }

        public IServiceProvider GetServiceProvider()
        {
            if (serviceProvider == null)
            {
                serviceProvider = serviceCollection.BuildServiceProvider();
                serviceCollection = null;
            }
            return serviceProvider;
        }

        public T GetService<T>() 
            => (T)GetServiceProvider().GetService(typeof(T));

        [Obsolete("You should not register service on ServiceLocator, use IServiceCollection instead")]
        public void RegisterTransient<T>(Func<T> factory)
        {
            RegisterService(factory, ServiceLifetime.Transient);
        }

        [Obsolete("You should not register service on ServiceLocator, use IServiceCollection instead")]
        public void RegisterSingleton<T>(Func<T> factory)
        {
            RegisterService(factory, ServiceLifetime.Singleton);
        }

        [Obsolete]
        private void RegisterService<T>(Func<T> factory, ServiceLifetime lifetime)
        {
            if (serviceCollection == null)
            {
                throw new InvalidOperationException("Could not register service to ServiceLocator that has already built IServiceProvider.");
            }

            serviceCollection.Add(new ServiceDescriptor(typeof(T), p => factory(), lifetime));
        }
    }
}<commit_msg>RegisterService obsolete with compile error check<commit_after>using System;
using Microsoft.Extensions.DependencyInjection;

namespace DotVVM.Framework.Configuration
{
    public class ServiceLocator
    {
        private IServiceCollection serviceCollection;
        private IServiceProvider serviceProvider;

        public ServiceLocator(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
        }

        public ServiceLocator(IServiceCollection serviceCollection)
        {
            this.serviceCollection = serviceCollection ?? throw new ArgumentNullException(nameof(serviceCollection));
        }

        public IServiceProvider GetServiceProvider()
        {
            if (serviceProvider == null)
            {
                serviceProvider = serviceCollection.BuildServiceProvider();
                serviceCollection = null;
            }
            return serviceProvider;
        }

        public T GetService<T>() 
            => (T)GetServiceProvider().GetService(typeof(T));

        [Obsolete("You should not register service on ServiceLocator, use IServiceCollection instead", true)]
        public void RegisterTransient<T>(Func<T> factory)
        {
            RegisterService(factory, ServiceLifetime.Transient);
        }

        [Obsolete("You should not register service on ServiceLocator, use IServiceCollection instead", true)]
        public void RegisterSingleton<T>(Func<T> factory)
        {
            RegisterService(factory, ServiceLifetime.Singleton);
        }

        [Obsolete("You should not register service on ServiceLocator, use IServiceCollection instead", true)]
        private void RegisterService<T>(Func<T> factory, ServiceLifetime lifetime)
        {
            if (serviceCollection == null)
            {
                throw new InvalidOperationException("Could not register service to ServiceLocator that has already built IServiceProvider.");
            }

            serviceCollection.Add(new ServiceDescriptor(typeof(T), p => factory(), lifetime));
        }
    }
}
{'fraction_non_alphanumeric': 0.07445086705202313, 'fraction_numerical': 0.0, 'mean_word_length': 2.4170616113744074, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '2886322', 'n_tokens_mistral': 970, 'n_tokens_neox': 937, 'n_words': 237}