- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the AddSingleton vs AddScoped vs Add Transient C# Asp.net Core?
There are three ways by which dependencies can be registered in Startup.cs. i.e. AddSingleton, AddScoped and AddTransient.
Add Singleton
When we register a type as singleton, only one instance is available throughout the application and for every request.
It is similar to having a static object.
The instance is created for the first request and the same is available throughout the application and for each subsequent requests.
public void ConfigureServices(IServiceCollection services){ services.AddSingleton<ILog,Logger>() }
Add Scoped
When we register a type as Scoped, one instance is available throughout the application per request. When a new request comes in, the new instance is created. Add scoped specifies that a single object is available per request.
public void ConfigureServices(IServiceCollection services){ services.AddScoped<ILog,Logger>() }
Add Transient
When we register a type as Transient, every time a new instance is created. Transient creates new instance for every service/ controller as well as for every request and every user.
public void ConfigureServices(IServiceCollection services){ services.AddTransient<ILog,Logger>() }
Parameter | Add Singleton | Add Scoped | Add Transient |
---|---|---|---|
Instance | Same each request/ each user. | One per request. | Different for everytime. |
Disposed | App shutdown | End of request | End of request |
Used in | When Singleton implementation is required. | Applications which have different behavior per user. | Light weight and stateless services. |
- Related Articles
- What is Kestral C# Asp.net Core?
- What is routing in C# ASP.NET Core?
- What is Metapackage in C# Asp.net Core?
- What is the use of UseIISIntegration in C# Asp.net Core?
- What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() C# Asp.net Core?
- What is the role of IWebHostEnvironment interface in C# ASP.NET Core?
- What are the benefits of choosing ASP.NET Core over ASP.NET?
- C++ vs C++0x vs C++11 vs C++98
- What is the purpose of Program.cs file in C# ASP.NET Core project?
- What is ASP.NET Core? Explain how it's different from the ASP.NET framework.
- C++ vs Java vs Python?
- Virtual vs Sealed vs New vs Abstract in C#
- How C# ASP.NET Core Middleware is different from HttpModule?
- What are the various JSON files available in C# ASP.NET Core?
- What is the use of the Configure() method of startup class in C# Asp.net Core?
