Skip to content

Instantly share code, notes, and snippets.

@salaros
Last active November 30, 2018 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salaros/ee1b53af45bd0ce50e9f21df32f1b05d to your computer and use it in GitHub Desktop.
Save salaros/ee1b53af45bd0ce50e9f21df32f1b05d to your computer and use it in GitHub Desktop.
IServiceProvider in ASP.NET Core dependency injection explained

Either approach can be used to get access to the service. Additional service extensions for Startup.cs

Instance

AddInstance<IService>(new Service())

A single instance is given all the time. You are responsible for initial object creation.

Singleton

AddSingleton<IService, Service>()

A single instance is created and it acts like a singleton.

Transient

AddTransient<IService, Service>()

A new instance is created every time it is injected.

Scoped

AddScoped<IService, Service>()

A scoped lifetime services are created once per request.

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