Exemplo n.º 1
0
        public void NoServiceTest()
        {
            IServiceProvider serviceProvider = _services.BuildServiceProvider();

            SomeSingleton       singleton           = serviceProvider.GetService <SomeSingleton>();
            SomeSecondTransient someSecondTransient = serviceProvider.GetService <SomeSecondTransient>();

            Assert.Null(singleton);
            Assert.Null(someSecondTransient);
        }
Exemplo n.º 2
0
        public void AddSingletonStructTest()
        {
            _services.AddSingleton <StructSingleton>(new StructSingleton(1234567));

            IServiceProvider provider = _services.BuildServiceProvider();

            StructSingleton first  = provider.GetService <StructSingleton>();
            StructSingleton second = provider.GetService <StructSingleton>();

            Assert.Equal(second, first);
            Assert.Equal(1234567, first.Value);
        }
Exemplo n.º 3
0
        public void AddTransient2Test()
        {
            _services.AddTransient <SomeTransient>(() => new SomeTransient());

            IServiceProvider provider = _services.BuildServiceProvider();

            SomeTransient first  = provider.GetService <SomeTransient>();
            SomeTransient second = provider.GetService <SomeTransient>();

            Assert.NotEqual(second, first);
            Assert.Equal(1, first.Counter);
            Assert.Equal(1, second.Counter);
        }
Exemplo n.º 4
0
        public void AddSingleton4Test()
        {
            _services.AddSingleton(provider => new SomeSingleton());

            IServiceProvider serviceProvider = _services.BuildServiceProvider();

            SomeSingleton first  = serviceProvider.GetService <SomeSingleton>();
            SomeSingleton second = serviceProvider.GetService <SomeSingleton>();

            Assert.Equal(second, first);
            Assert.Equal(1, first.Counter);
            Assert.Equal(1, second.Counter);
        }
Exemplo n.º 5
0
        public void AddSingletonRecordTest()
        {
            _services.AddSingleton <RecordSingleton>(new RecordSingleton(987654));

            IServiceProvider provider = _services.BuildServiceProvider();

            RecordSingleton first  = provider.GetService <RecordSingleton>();
            RecordSingleton second = provider.GetService <RecordSingleton>();

            Assert.Same(second, first);
            Assert.Equal(second, first);
            Assert.Equal(987654, first.SomeValue);
        }
Exemplo n.º 6
0
        public void AddSingleton1Test()
        {
            _services.AddSingleton <SomeSingleton>();

            IServiceProvider provider = _services.BuildServiceProvider();

            SomeSingleton first  = provider.GetService <SomeSingleton>();
            SomeSingleton second = provider.GetService <SomeSingleton>();

            Assert.Equal(second, first);
            Assert.Same(second, first);
            Assert.Equal(1, first.Counter);
            Assert.Equal(1, second.Counter);
        }
Exemplo n.º 7
0
        public void AddSingleton1Test()
        {
            IServiceCollection services = new ServiceCollection();

            services.AddSingleton <SomeSingleton>();

            IServiceProvider provider = services.BuildServiceProvider();

            SomeSingleton first  = provider.GetService <SomeSingleton>();
            SomeSingleton second = provider.GetService <SomeSingleton>();

            Assert.Equal(second, first);
            Assert.Equal(1, first.Counter);
            Assert.Equal(1, second.Counter);
        }
Exemplo n.º 8
0
        public void ComplexTest()
        {
            _services.AddSingleton <SomeSingleton>();
            _services.AddTransient(provider => new SomeSecondTransient(provider.GetService <SomeSingleton>()));

            IServiceProvider serviceProvider = _services.BuildServiceProvider();

            SomeSingleton       sing   = serviceProvider.GetService <SomeSingleton>();
            SomeSecondTransient first  = serviceProvider.GetService <SomeSecondTransient>();
            SomeSecondTransient second = serviceProvider.GetService <SomeSecondTransient>();

            Assert.NotEqual(second, first);
            Assert.Equal(1, first.Counter);
            Assert.Equal(1, second.Counter);
            Assert.Equal(1, sing.Counter);
        }
Exemplo n.º 9
0
        public void LastRegistrationUsedTest()
        {
            _services.AddSingleton <SomeSingleton>(() => throw new InvalidOperationException());
            _services.AddSingleton <SomeSingleton>(() => throw new ArgumentNullException());
            _services.AddSingleton <SomeSingleton>();

            IServiceProvider provider = _services.BuildServiceProvider();

            SomeSingleton first  = provider.GetService <SomeSingleton>();
            SomeSingleton second = provider.GetService <SomeSingleton>();

            Assert.Equal(second, first);
            Assert.Same(second, first);
            Assert.Equal(1, first.Counter);
            Assert.Equal(1, second.Counter);
        }
Exemplo n.º 10
0
        public void DifferentRegistrationsTest()
        {
            _services.AddSingleton <SomeSingleton>();
            _services.AddTransient <SomeSingleton>();
            _services.AddSingleton <SomeSingleton>();

            IServiceProvider provider = _services.BuildServiceProvider();

            SomeSingleton first  = provider.GetService <SomeSingleton>();
            SomeSingleton second = provider.GetService <SomeSingleton>();

            Assert.Equal(second, first);
            Assert.Same(second, first);
            Assert.Equal(1, first.Counter);
            Assert.Equal(1, second.Counter);
        }
Exemplo n.º 11
0
        public void ExceptionFactory3Test()
        {
            _services.AddTransient <SomeTransient>(p => throw new NetworkInformationException());

            IServiceProvider provider = _services.BuildServiceProvider();

            Assert.Throws <NetworkInformationException>(() => provider.GetService <SomeSingleton>());
        }