Skip to content

Instantly share code, notes, and snippets.

@xiaomi7732
Created August 8, 2023 00:02
Show Gist options
  • Save xiaomi7732/07fe85c4fe80387d0318c1740ae8d078 to your computer and use it in GitHub Desktop.
Save xiaomi7732/07fe85c4fe80387d0318c1740ae8d078 to your computer and use it in GitHub Desktop.
How to replace a service in C# dependency injection

How to replace a service in C# dependency injection

Discover the Power of the Replace Method!

Unveil the hidden gem within C# dependency injection – the Replace method – to effortlessly update services. Experience the simplicity firsthand:

Suppose you're aiming to substitute the initial singleton service of IAnalyzer with the innovative SimpleAnalyzer implementation. Behold the elegance of the solution:

services.Replace(ServiceDescriptor.Singleton<IAnalyzer, SimpleAnalyzer>());

No more intricate processes – just seamless service replacement for your C# applications!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment