# Dependency Injection (.NET) > 서비스 라이프타임(singleton, scoped, transient), 서비스 등록, IServiceProvider, 모범 사례 - 24 면접 질문 - Mid-Level - [면접 질문: .NET](https://sharpskill.dev/ko/technologies/dotnet/interview-questions.md) ## 1. Singleton, Scoped, Transient 라이프타임의 주요 차이점은 무엇입니까? **답변** Singleton은 애플리케이션 라이프타임 전체에 대해 단일 인스턴스를 생성하고, Scoped는 HTTP 요청(또는 스코프)당 하나의 인스턴스를 생성하며, Transient는 주입될 때마다 새 인스턴스를 생성합니다. 이러한 라이프타임은 DI 컨테이너가 서비스를 언제, 몇 번 인스턴스화하는지를 결정합니다. 상태가 없는 공유 서비스에는 Singleton, 요청에 바인딩된 서비스(DbContext 등)에는 Scoped, 상태가 없는 경량 서비스에는 Transient를 사용합니다. ## 2. Entity Framework Core DbContext를 등록하려면 어떤 lifetime을 사용해야 하나요? **답변** DbContext는 thread-safe하지 않고 엔티티 추적 상태를 유지하므로 Scoped lifetime으로 등록해야 합니다. 동시성 문제를 피하려면 각 HTTP 요청마다 자체 DbContext 인스턴스를 가져야 합니다. 등록은 AddDbContext로 하며, 이것이 자동으로 Scoped lifetime을 구성합니다. Singleton을 사용하면 동시성 오류가 발생하고, Transient는 너무 많은 인스턴스를 생성해 리소스를 낭비합니다. ## 3. interface와 그 구현을 사용하여 서비스를 등록하는 방법은 무엇인가요? **답변** 서비스 등록은 AddSingleton, AddScoped 또는 AddTransient 메서드로 interface를 서비스 타입으로, 구체 클래스를 구현으로 지정하여 수행합니다. 구문은 services.Add{Lifetime}()입니다. 이 방식은 의존성 역전 원칙을 따르며, 구현을 대체할 수 있게 하여 테스트를 용이하게 합니다. DI 컨테이너는 interface를 주입할 때 구체 구현을 자동으로 해결합니다. ## 21개 추가 질문 이용 가능 - AddScoped보다 AddSingleton을 선호해야 하는 경우는 언제인가요? - DI 시스템에서 IServiceProvider의 역할은 무엇인가요? 무료로 가입하기: https://sharpskill.dev/ko/login ## 기타 .NET 면접 주제 - [C# 기초](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/csharp-basics.md): 25개 질문, Junior - [LINQ & Delegates](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/csharp-linq-delegates.md): 20개 질문, Junior - [C# 언어 핵심 요소](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/csharp-language-essentials.md): 15개 질문, Junior - [ASP.NET Core 기초](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/aspnet-core-fundamentals.md): 18개 질문, Junior - [ASP.NET Core 요청 수명 주기](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/aspnet-core-request-lifecycle.md): 20개 질문, Junior - [Configuration & Settings](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/configuration-settings.md): 20개 질문, Junior - [애플리케이션 라이프사이클](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/application-lifecycle.md): 20개 질문, Junior - [Entity Framework Core](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/entity-framework-core.md): 25개 질문, Mid-Level - [Minimal APIs](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/minimal-apis.md): 18개 질문, Mid-Level - [Web API 개발](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/web-api-development.md): 22개 질문, Mid-Level - [ASP.NET Core의 Async](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/async-aspnet-core.md): 20개 질문, Mid-Level - [Authentication & Authorization](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/authentication-authorization.md): 18개 질문, Mid-Level - [HttpClient & 네트워킹](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/httpclient-networking.md): 20개 질문, Mid-Level - [JSON 직렬화](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/json-serialization.md): 20개 질문, Mid-Level - [Entity Framework Core 고급](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/ef-core-advanced.md): 25개 질문, Mid-Level - [C# 고급 기능](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/csharp-advanced-features.md): 20개 질문, Mid-Level - [Clean Architecture](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/clean-architecture.md): 20개 질문, Mid-Level - [Logging, Monitoring & Observability](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/logging-monitoring.md): 22개 질문, Mid-Level - [단위 테스트와 xUnit](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/unit-testing-xunit.md): 20개 질문, Mid-Level - [통합 테스트](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/integration-testing.md): 18개 질문, Mid-Level - [Docker & Containerization](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/docker-containerization.md): 16개 질문, Mid-Level - [NuGet 패키지 관리](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/nuget-package-management.md): 16개 질문, Mid-Level - [Memory Management & GC](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/memory-management-gc.md): 22개 질문, Senior - [리액티브 프로그래밍](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/reactive-programming.md): 24개 질문, Senior - [Async/await 및 패턴](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/async-await-patterns.md): 26개 질문, Senior - [.NET 디자인 패턴](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/dotnet-design-patterns.md): 24개 질문, Senior - [성능 최적화](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/performance-optimization.md): 22개 질문, Senior - [보안 및 모범 사례](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/security-best-practices.md): 24개 질문, Senior - [SignalR 및 실시간](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/signalr-realtime.md): 22개 질문, Senior - [마이크로서비스 아키텍처](https://sharpskill.dev/ko/technologies/dotnet/interview-questions/microservices-architecture.md): 25개 질문, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/ko/technologies/dotnet/interview-questions/dependency-injection