This is simple using the Replace(IServiceCollection, ServiceDescriptor)
method from the ServiceCollectionDescriptorExtensions
class.
// IFoo -> FooA
services.AddTransient<IFoo, FooA>();
// Replace
// IFoo -> FooB
var descriptor =
new ServiceDescriptor(
typeof(IFoo),
typeof(FooB),
ServiceLifetime.Transient);
services.Replace(descriptor);
See also:
- ServiceDescriptor constructors