Beispiel #1
0
        public void ReplaceScopedShouldReplaceServiceByGenericTypeAndImplementationFactory()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddScoped(typeof(IInjectedService), typeof(InjectedService));

            Assert.IsAssignableFrom <InjectedService>(serviceCollection.BuildServiceProvider().GetService <IInjectedService>());

            var service = new ReplaceableInjectedService();

            serviceCollection.ReplaceScoped <IInjectedService>(s => service);

            Assert.Same(service, serviceCollection.BuildServiceProvider().GetService <IInjectedService>());
        }
Beispiel #2
0
        public void ReplaceSingletonShouldReplaceServiceByTypeAndImplementationInstance()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(typeof(IInjectedService), typeof(InjectedService));

            Assert.IsAssignableFrom <InjectedService>(serviceCollection.BuildServiceProvider().GetService <IInjectedService>());

            var service = new ReplaceableInjectedService();

            serviceCollection.ReplaceSingleton(typeof(IInjectedService), service);

            Assert.Same(service, serviceCollection.BuildServiceProvider().GetService <IInjectedService>());
        }
Beispiel #3
0
        public void ReplaceTransientShouldReplaceServiceByGenericAndImplementationFactory()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddTransient <IInjectedService, InjectedService>();

            Assert.IsAssignableFrom <InjectedService>(serviceCollection.BuildServiceProvider().GetService <IInjectedService>());

            var service = new ReplaceableInjectedService();

            serviceCollection.ReplaceTransient <IInjectedService>(s => service);

            Assert.Same(service, serviceCollection.BuildServiceProvider().GetService <IInjectedService>());
        }
Beispiel #4
0
        public void ReplaceShouldReplaceServiceByTypeAndImplementationFactory()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddTransient(typeof(IInjectedService), typeof(InjectedService));

            Assert.IsAssignableFrom <InjectedService>(serviceCollection.BuildServiceProvider().GetService <IInjectedService>());

            var injectedService = new ReplaceableInjectedService();

            serviceCollection.Replace(typeof(IInjectedService), s => injectedService, ServiceLifetime.Singleton);

            var actualService = serviceCollection.FirstOrDefault(s => s.ServiceType == typeof(IInjectedService) && s.Lifetime == ServiceLifetime.Singleton);

            Assert.NotNull(actualService);

            Assert.Same(injectedService, serviceCollection.BuildServiceProvider().GetService <IInjectedService>());
        }
        public void ReplaceScopedShouldReplaceServiceByGenericTypeAndImplementationFactory()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddScoped(typeof(IInjectedService), typeof(InjectedService));

            Assert.IsAssignableFrom<InjectedService>(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());

            var service = new ReplaceableInjectedService();
            serviceCollection.ReplaceScoped<IInjectedService>(s => service);

            Assert.Same(service, serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
        }
        public void ReplaceSingletonShouldReplaceServiceByTypeAndImplementationInstance()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddSingleton(typeof(IInjectedService), typeof(InjectedService));

            Assert.IsAssignableFrom<InjectedService>(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());

            var service = new ReplaceableInjectedService();
            serviceCollection.ReplaceSingleton(typeof(IInjectedService), service);

            Assert.Same(service, serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
        }
        public void ReplaceTransientShouldReplaceServiceByGenericAndImplementationFactory()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddTransient<IInjectedService, InjectedService>();

            Assert.IsAssignableFrom<InjectedService>(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());

            var service = new ReplaceableInjectedService();
            serviceCollection.ReplaceTransient<IInjectedService>(s => service);

            Assert.Same(service, serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
        }
        public void ReplaceShouldReplaceServiceByTypeAndImplementationFactory()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddTransient(typeof(IInjectedService), typeof(InjectedService));

            Assert.IsAssignableFrom<InjectedService>(serviceCollection.BuildServiceProvider().GetService<IInjectedService>());

            var injectedService = new ReplaceableInjectedService();
            serviceCollection.Replace(typeof(IInjectedService), s => injectedService, ServiceLifetime.Singleton);

            var actualService = serviceCollection.FirstOrDefault(s => s.ServiceType == typeof(IInjectedService) && s.Lifetime == ServiceLifetime.Singleton);
            Assert.NotNull(actualService);

            Assert.Same(injectedService, serviceCollection.BuildServiceProvider().GetService<IInjectedService>());
        }