Stage 2 - Core Services and Configuration
Q11. What is Dependency Injection in ASP.NET Core?
Quick interview answer: Dependency Injection is a pattern where required services are provided by ASP.NET Core instead of being created manually inside classes. It reduces coupling and makes code easier to test.
Study in detail: Dependency Injection (DI) - This article explains registration, injection, and lifetimes.
Q12. What are Transient, Scoped, and Singleton lifetimes?
Quick interview answer:
Transient creates a new instance every time. Scoped creates one instance per HTTP request. Singleton creates one instance for the whole application.
Transient = every request for service
Scoped = one per HTTP request
Singleton = one for app lifetime
Study in detail: Dependency Injection (DI) - This article explains lifetime selection.
Q13. Which lifetime is commonly used for business services?
Quick interview answer:
Scoped is commonly used for business services, repositories, and database-related services because one consistent instance is used during a single HTTP request.
Study in detail: Dependency Injection (DI) - This article explains real service examples.
Q14. Why should we not inject scoped services into singleton services?
Quick interview answer: A singleton lives for the entire application, while a scoped service lives only for one request. Injecting scoped into singleton can capture request-specific state and cause incorrect behavior.
Study in detail: Dependency Injection (DI) - This article explains lifetime mistakes.
Q15. What is appsettings.json?
Quick interview answer:
appsettings.json is a configuration file used to store settings such as connection strings, logging levels, feature flags, API settings, and application options.
Study in detail: Configuration and appsettings.json - This article explains configuration sources and reading values.
Q16. How do you read configuration values?
Quick interview answer:
Use builder.Configuration or inject IConfiguration.
var connectionString = builder.Configuration.GetConnectionString("SchoolDb");
var schoolName = builder.Configuration["School:Name"];
Study in detail: Configuration and appsettings.json - This article explains configuration keys and Options pattern.
Q17. What are environments in ASP.NET Core?
Quick interview answer: Environments let the same application behave differently in Development, Staging, and Production. For example, development can show detailed errors, while production should show safe messages.
Study in detail: Environments - Development, Staging, Production - This article explains environment-specific behavior.
Q18. How do you check the current environment?
Quick interview answer:
Use app.Environment.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
Study in detail: Environments - Development, Staging, Production - This article explains IsDevelopment, IsStaging, and IsProduction.
Q19. What is logging in ASP.NET Core?
Quick interview answer:
Logging records application events, warnings, and errors. ASP.NET Core uses ILogger<T> and supports different log levels such as Information, Warning, Error, and Critical.
Study in detail: Logging with ILogger - This article explains log levels and structured logging.
Q20. What is structured logging?
Quick interview answer: Structured logging stores message values separately using placeholders, making logs easier to search and filter.
_logger.LogInformation("Student {StudentId} loaded", id);
Study in detail: Logging with ILogger - This article explains logging best practices.