Example #1
0
        public void WaitLimitAndTimeout()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransientPool <ISingleService, SingleService>(o =>
            {
                o.PoolMaxSize = 10;
                o.ExceedLimitWhenWaitTimeout = false;
                o.WaitAvailableDuration      = TimeSpan.FromMilliseconds(1500);
            });

            IContainerScope scope = services.CreateScope();

            for (int i = 0; i < 10; i++)
            {
                ISingleService service = services.Get <ISingleService>(scope);
                Assert.NotNull(service);
            }

            DateTime start = DateTime.UtcNow;

            Assert.Throws <NullReferenceException>(() => services.Get <ISingleService>(scope));
            DateTime end = DateTime.UtcNow;

            Assert.True(end - start > TimeSpan.FromMilliseconds(1490));
        }
Example #2
0
        public async Task LongRunning()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScopedPool <ISingleService, SingleService>();

            for (int i = 0; i < 50; i++)
            {
                IContainerScope scope1 = services.CreateScope();
                IContainerScope scope2 = services.CreateScope();

                ISingleService service1 = await services.Get <ISingleService>(scope1);

                ISingleService service2 = await services.Get <ISingleService>(scope2);

                Assert.NotNull(service1);
                Assert.NotNull(service2);
                Assert.NotEqual(service1, service2);

                await Task.Delay(10);

                scope1.Dispose();
                scope2.Dispose();
            }
        }
Example #3
0
        public async void Single()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <ISingleService, SingleService>();

            ISingleService s1 = await services.Get <ISingleService>();

            s1.Foo = "a";

            //s1 and s2 should not be equal because they should be different instances
            ISingleService s2 = await services.Get <ISingleService>();

            Assert.NotEqual(s1.Foo, s2.Foo);

            //s1 or s2 should not equal to scoped transient instance
            IContainerScope scope = services.CreateScope();
            ISingleService  s3    = await services.Get <ISingleService>(scope);

            Assert.NotEqual(s1.Foo, s3.Foo);
            s3.Foo = "b";

            //two transient instances in same scope should not be equal
            ISingleService s4 = await services.Get <ISingleService>(scope);

            Assert.NotEqual(s1.Foo, s4.Foo);
            Assert.NotEqual(s3.Foo, s4.Foo);
        }
Example #4
0
        public async void Single()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScoped <ISingleService, SingleService>();

            IContainerScope scope = services.CreateScope();

            ISingleService s1 = await services.Get <ISingleService>(scope);

            s1.Foo = "a";

            ISingleService s2 = await services.Get <ISingleService>(scope);

            Assert.Equal(s1.Foo, s2.Foo);

            //scopeless should throw error
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISingleService>());

            //another scope should not equal
            IContainerScope scope2 = services.CreateScope();
            ISingleService  s4     = await services.Get <ISingleService>(scope2);

            Assert.NotEqual(s1.Foo, s4.Foo);
            Assert.NotEqual(s2.Foo, s4.Foo);
        }
Example #5
0
        public void Single()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScoped <ISingleService, SingleService>();

            IContainerScope scope = services.CreateScope();

            ISingleService s1 = services.Get <ISingleService>(scope);

            s1.Foo = "a";

            ISingleService s2 = services.Get <ISingleService>(scope);

            Assert.Equal(s1.Foo, s2.Foo);

            //scopeless should throw error
            Assert.Throws <ScopeException>(() => services.Get <ISingleService>());

            //another scope should not equal
            IContainerScope scope2 = services.CreateScope();
            ISingleService  s4     = services.Get <ISingleService>(scope2);

            Assert.NotEqual(s1.Foo, s4.Foo);
            Assert.NotEqual(s2.Foo, s4.Foo);
        }
Example #6
0
        public void Transient()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransientPool <ISingleService, SingleService>();

            IContainerScope scope = services.CreateScope();

            ISingleService single = services.Get <ISingleService>(scope);

            single.Foo = "single";

            ISingleService s1 = services.Get <ISingleService>(scope);

            Assert.NotEqual(single.Foo, s1.Foo);

            ISingleService s2 = services.Get <ISingleService>();

            Assert.NotEqual(single.Foo, s2.Foo);

            IContainerScope scope2 = services.CreateScope();
            ISingleService  s3     = services.Get <ISingleService>(scope2);

            Assert.NotEqual(single.Foo, s3.Foo);
        }
Example #7
0
        public void MultipleNestedDoubleParameter()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScoped <INestParentService, NestParentService>();
            services.AddScoped <ISingleService, SingleService>();
            services.AddScoped <IParentService, ParentService>();
            services.AddScoped <IFirstChildService, FirstChildService>();
            services.AddScoped <ISecondChildService, SecondChildService>();

            IContainerScope scope = services.CreateScope();

            INestParentService nest = services.Get <INestParentService>(scope);

            nest.Foo               = "nest";
            nest.Parent.Foo        = "parent";
            nest.Parent.First.Foo  = "first";
            nest.Parent.Second.Foo = "second";
            nest.Single.Foo        = "single";

            INestParentService n1 = services.Get <INestParentService>(scope);

            Assert.Equal(nest.Foo, n1.Foo);
            Assert.Equal(nest.Single.Foo, n1.Single.Foo);
            Assert.Equal(nest.Parent.Foo, n1.Parent.Foo);
            Assert.Equal(nest.Parent.First.Foo, n1.Parent.First.Foo);
            Assert.Equal(nest.Parent.Second.Foo, n1.Parent.Second.Foo);

            IParentService parent = services.Get <IParentService>(scope);

            Assert.Equal(nest.Parent.Foo, parent.Foo);
            Assert.Equal(nest.Parent.First.Foo, parent.First.Foo);
            Assert.Equal(nest.Parent.Second.Foo, parent.Second.Foo);

            ISingleService single = services.Get <ISingleService>(scope);

            Assert.Equal(nest.Single.Foo, single.Foo);

            IFirstChildService first = services.Get <IFirstChildService>(scope);

            Assert.Equal(nest.Parent.First.Foo, first.Foo);

            ISecondChildService second = services.Get <ISecondChildService>(scope);

            Assert.Equal(nest.Parent.Second.Foo, second.Foo);

            //scopeless should throw error
            Assert.Throws <ScopeException>(() => services.Get <INestParentService>());

            //another scope should not equal
            IContainerScope    scope2 = services.CreateScope();
            INestParentService n3     = services.Get <INestParentService>(scope2);

            Assert.NotEqual(nest.Foo, n3.Foo);
            Assert.NotEqual(nest.Single.Foo, n3.Single.Foo);
            Assert.NotEqual(nest.Parent.Foo, n3.Parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, n3.Parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, n3.Parent.Second.Foo);
        }
Example #8
0
        public async Task MultipleNestedDoubleParameter()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddSingleton <INestParentService, NestParentService>();
            services.AddSingleton <ISingleService, SingleService>();
            services.AddSingleton <IParentService, ParentService>();
            services.AddSingleton <IFirstChildService, FirstChildService>();
            services.AddSingleton <ISecondChildService, SecondChildService>();

            INestParentService nest = await services.Get <INestParentService>();

            nest.Foo               = "nest";
            nest.Parent.Foo        = "parent";
            nest.Parent.First.Foo  = "first";
            nest.Parent.Second.Foo = "second";
            nest.Single.Foo        = "single";

            INestParentService n1 = await services.Get <INestParentService>();

            Assert.Equal(nest.Foo, n1.Foo);
            Assert.Equal(nest.Single.Foo, n1.Single.Foo);
            Assert.Equal(nest.Parent.Foo, n1.Parent.Foo);
            Assert.Equal(nest.Parent.First.Foo, n1.Parent.First.Foo);
            Assert.Equal(nest.Parent.Second.Foo, n1.Parent.Second.Foo);

            IParentService parent = await services.Get <IParentService>();

            Assert.Equal(nest.Parent.Foo, parent.Foo);
            Assert.Equal(nest.Parent.First.Foo, parent.First.Foo);
            Assert.Equal(nest.Parent.Second.Foo, parent.Second.Foo);

            ISingleService single = await services.Get <ISingleService>();

            Assert.Equal(nest.Single.Foo, single.Foo);

            IFirstChildService first = await services.Get <IFirstChildService>();

            Assert.Equal(nest.Parent.First.Foo, first.Foo);

            ISecondChildService second = await services.Get <ISecondChildService>();

            Assert.Equal(nest.Parent.Second.Foo, second.Foo);

            IContainerScope    scope = services.CreateScope();
            INestParentService n2    = await services.Get <INestParentService>(scope);

            Assert.Equal(nest.Foo, n2.Foo);
            Assert.Equal(nest.Single.Foo, n2.Single.Foo);
            Assert.Equal(nest.Parent.Foo, n2.Parent.Foo);
            Assert.Equal(nest.Parent.First.Foo, n2.Parent.First.Foo);
            Assert.Equal(nest.Parent.Second.Foo, n2.Parent.Second.Foo);
        }
Example #9
0
        public async Task Single()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddSingleton <ISingleService, SingleService>();

            ISingleService singleton = await services.Get <ISingleService>();

            singleton.Foo = "singleton";

            ISingleService s1 = await services.Get <ISingleService>();

            Assert.Equal(singleton.Foo, s1.Foo);

            IContainerScope scope = services.CreateScope();
            ISingleService  s2    = await services.Get <ISingleService>(scope);

            Assert.Equal(singleton.Foo, s2.Foo);
        }
        public void Instanced()
        {
            SingleService singleService = new SingleService();

            singleService.Foo = "singleton";

            ServiceContainer services = new ServiceContainer();

            services.AddSingleton <ISingleService, SingleService>(singleService);

            ISingleService s1 = services.Get <ISingleService>();

            Assert.Equal(singleService.Foo, s1.Foo);

            IContainerScope scope = services.CreateScope();
            ISingleService  s2    = services.Get <ISingleService>(scope);

            Assert.Equal(singleService.Foo, s2.Foo);
        }
Example #11
0
        public async Task WaitLimitAndGet()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransientPool <ISingleService, SingleService>(o =>
            {
                o.PoolMaxSize = 10;
                o.ExceedLimitWhenWaitTimeout = false;
                o.WaitAvailableDuration      = TimeSpan.FromMilliseconds(5000);
            });

            IContainerScope scope = services.CreateScope();

            for (int i = 0; i < 10; i++)
            {
                ISingleService service = await services.Get <ISingleService>(scope);

                Assert.NotNull(service);
            }

            DateTime start = DateTime.UtcNow;
            Thread   th    = new Thread(() =>
            {
                Thread.Sleep(500);
                scope.Dispose();
            });

            th.Start();

            IContainerScope scope2 = services.CreateScope();
            ISingleService  s      = await services.Get <ISingleService>(scope2);

            Assert.NotNull(s);

            DateTime end  = DateTime.UtcNow;
            TimeSpan time = end - start;

            Assert.True(time > TimeSpan.FromMilliseconds(490));
            Assert.True(time < TimeSpan.FromMilliseconds(750));
        }
Example #12
0
        public async Task Scoped()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScopedPool <ISingleService, SingleService>();

            IContainerScope scope = services.CreateScope();

            ISingleService single = await services.Get <ISingleService>(scope);

            single.Foo = "single";

            ISingleService s1 = await services.Get <ISingleService>(scope);

            Assert.Equal(single.Foo, s1.Foo);

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await services.Get <ISingleService>());

            IContainerScope scope2 = services.CreateScope();
            ISingleService  s3     = await services.Get <ISingleService>(scope2);

            Assert.NotEqual(single.Foo, s3.Foo);
        }
Example #13
0
        public void Scoped()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddScopedPool <ISingleService, SingleService>();

            IContainerScope scope = services.CreateScope();

            ISingleService single = services.Get <ISingleService>(scope);

            single.Foo = "single";

            ISingleService s1 = services.Get <ISingleService>(scope);

            Assert.Equal(single.Foo, s1.Foo);

            Assert.Throws <ScopeException>(() => services.Get <ISingleService>());

            IContainerScope scope2 = services.CreateScope();
            ISingleService  s3     = services.Get <ISingleService>(scope2);

            Assert.NotEqual(single.Foo, s3.Foo);
        }
        public void MultipleNestedDoubleParameter()
        {
            ServiceContainer services = new ServiceContainer();

            services.AddTransient <INestParentService, NestParentService>();
            services.AddTransient <ISingleService, SingleService>();
            services.AddTransient <IParentService, ParentService>();
            services.AddTransient <IFirstChildService, FirstChildService>();
            services.AddTransient <ISecondChildService, SecondChildService>();

            INestParentService nest = services.Get <INestParentService>();

            nest.Foo               = "nest";
            nest.Parent.Foo        = "parent";
            nest.Parent.First.Foo  = "first";
            nest.Parent.Second.Foo = "second";
            nest.Single.Foo        = "single";

            INestParentService n1 = services.Get <INestParentService>();

            Assert.NotEqual(nest.Foo, n1.Foo);
            Assert.NotEqual(nest.Single.Foo, n1.Single.Foo);
            Assert.NotEqual(nest.Parent.Foo, n1.Parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, n1.Parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, n1.Parent.Second.Foo);

            IParentService parent = services.Get <IParentService>();

            Assert.NotEqual(nest.Parent.Foo, parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, parent.Second.Foo);

            ISingleService single = services.Get <ISingleService>();

            Assert.NotEqual(nest.Single.Foo, single.Foo);

            IFirstChildService first = services.Get <IFirstChildService>();

            Assert.NotEqual(nest.Parent.First.Foo, first.Foo);

            ISecondChildService second = services.Get <ISecondChildService>();

            Assert.NotEqual(nest.Parent.Second.Foo, second.Foo);

            IContainerScope scope = services.CreateScope();

            INestParentService n2 = services.Get <INestParentService>(scope);

            Assert.NotEqual(nest.Foo, n2.Foo);
            Assert.NotEqual(nest.Single.Foo, n2.Single.Foo);
            Assert.NotEqual(nest.Parent.Foo, n2.Parent.Foo);
            Assert.NotEqual(nest.Parent.First.Foo, n2.Parent.First.Foo);
            Assert.NotEqual(nest.Parent.Second.Foo, n2.Parent.Second.Foo);
            n2.Foo               = "nest-2";
            n2.Parent.Foo        = "parent-2";
            n2.Parent.First.Foo  = "first-2";
            n2.Parent.Second.Foo = "second-2";
            n2.Single.Foo        = "single-2";

            INestParentService n3 = services.Get <INestParentService>(scope);

            Assert.NotEqual(n2.Foo, n3.Foo);
            Assert.NotEqual(n2.Single.Foo, n3.Single.Foo);
            Assert.NotEqual(n2.Parent.Foo, n3.Parent.Foo);
            Assert.NotEqual(n2.Parent.First.Foo, n3.Parent.First.Foo);
            Assert.NotEqual(n2.Parent.Second.Foo, n3.Parent.Second.Foo);
        }
Example #15
0
 public ProcessingManager(IMessageHandler messageHandler, IMessagePublisher messagepublisher, ISingleService singleservice)
 {
     _messageHandler   = messageHandler;
     _messagePublisher = messagepublisher;
     _service          = singleservice;
 }
Example #16
0
 public NestParentService(IParentService parentService, ISingleService singleService)
 {
     Parent = parentService;
     Single = singleService;
 }
Example #17
0
 public SingleController(ISingleService singleService)
 {
     Guard.WhenArgument(singleService, "singleService").IsNull().Throw();
     this.singleService = singleService;
 }