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>()); }
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>()); }