Replies: 7 comments
-
What is already registered that you are trying to register? |
Beta Was this translation helpful? Give feedback.
-
I don't have a specific case, but when somebody uses your Startup.cs from the template and also adds some services on his own it might be better if at least the template stuff uses |
Beta Was this translation helpful? Give feedback.
-
I assume that doing this would cause an exception and then they could remove the line that they added because it's not needed. |
Beta Was this translation helpful? Give feedback.
-
I don't think exceptions are thrown. I tried services.AddSingleton<IClass1, Class1>(provider => new Class1());
services.AddSingleton<IClass1, Class1>(provider => new Class1());
services.AddSingleton<IClass1, Class1B>(provider => new Class1B()); and only the constructor from Now when I have services.AddSingleton<IClass1, Class1>(provider => new Class1());
services.AddSingleton<IClass1, Class1>(provider => new Class1());
services.TryAddSingleton(typeof(IClass1), provider => new Class1B()); only the |
Beta Was this translation helpful? Give feedback.
-
Right, I think by default if the service is already there it gets replaced, the TryAdd only adds it if it's not already been added. You are worried that someone will override one of the services that have been registered and it will get replaced. I don't think there would be any bad consequences. |
Beta Was this translation helpful? Give feedback.
-
Yeah, that's what I'm thinking. Maybe a custom I don't have any good use cases. But maybe using |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, we can only do so much to stop developers shooting themselves in the foot. The backlog on this project is getting a little large, without looking at an edge case like this. |
Beta Was this translation helpful? Give feedback.
-
I think this might be better in case a service has already been registered. Unfortunately there are no generic overloads.
Beta Was this translation helpful? Give feedback.
All reactions