[.net] Singleton vs Transient 오류

해질녘·2022년 3월 10일
0

닷넷 (.NET)

목록 보기
9/12

오류 메시지

Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: muscatApi.Models.muscatApiContext Lifetime: Singleton ImplementationType: muscatApi.Models.muscatApiContext': Cannot consume scoped service 

객체를 관리하기 위해 Program.csAddSingleton 했더니 오류가 나온다.

해결

이 부분을 삭제하면 연결 됨. 혹은, AddTransient로 고쳐도 됨.

기본적으로 명시되지 않으면 dbcontext는 transient로 관리된다.

따라서 이 오류는 dbcontext가 transient인데, 그걸 싱글턴 (더 긴 수명)으로 관리하려고 하니 발생하는 것.

efcore의 dbcontext는 singleton을 권장하지 않는다. 메모리 누수 등의 문제가 있기 때문이다.

profile
해질녘 | 백엔드 공부 중

0개의 댓글