-
Notifications
You must be signed in to change notification settings - Fork 320
Add a way to replace services in an IServiceCollection #155
Comments
Is this needed to replace framework registered services with custom implementation? |
It is needed to do it easily in a way that will work with all IoC containers (such as Ninject). As I mention in the issue, you can always create a new |
@halter73 what you need replace service for? |
There are many framework services that have default implementations that need to be replaced in some scenarios. For example, you might want to replace SignalR's default in-memory |
It appears to me that this way of replacing services is not well discoverable, I would like change something on a configuration object, not replace a servicedescriptor in a collection of service descriptors. Since almost every repo here uses the pattern services.AddXXX() to register default services, this could accept a configuration object where I can replace default services. I think this is a simpler and more discoverable approach. |
I should've read this thread before jumping the gun. Based on @davidfowl 's suggestion I added a That said, the semantics of how Replace is currently implemented is iffy when multiple services of a particular service type are registered and it needs to be replaced. Should
|
3 works for me, as it works correctly in most cases, and throws when something unexpected happens. But I see how 2 is simpler and also works in more cases. 1 is just weird. When it comes to configuration
At least in MVC we are strongly encouraging users not to replace services (that's a last resort large hammer approach), and we attempt provide all the required extensibility through |
@yishaigalatzer I hope this Replace will throw if called after the container is fully configured (e.g after serving the first instance or after manually locking the configuration) otherwise I think it will be a source of pain.
I wasn't referring to service replacement, but instead to service registration. All user replaceable services can be put on a configuration object used by MVC for services registration. |
Replace is on the service collection not the the service provider |
|
@davidfowl @yishaigalatzer Thanks, this all make sense now. |
There are |
Currently, it isn't possible to replace or remove
IServiceDescriptors
in anIServiceCollection
. The best you can do is to copy into a new collection only the descriptors you want to keep unmodified. Then you can add new descriptors to replace any descriptors from the original collection that you didn't copy over. This is a pain.Previously, I thought we could work around this issue by depending on IoC containers to only inject the last registered service. Unfortunately, Ninject will fail to inject a single service into a constructor if it is registered multiple times. This work-around also doesn't address how to remove/replace services that are resolved using
IEnumerable<TService>
.The
TryAdd
extension method allows you to only add services if they are not yet defined in theIServiceCollection
, but offers no help if you want to replace services already in the collection.The text was updated successfully, but these errors were encountered: