Vòng đời · captive dependency

DbContext bị Singleton bắt giữ sống bao lâu

Vòng đời DbContext khi bị Singleton bắt giữ DbContext nằm trong field của một Singleton sống từ lần resolve đầu tiên tới lúc app tắt, trong khi DbContext theo request hoặc theo scope tự mở bằng IServiceScopeFactory được dispose ngay khi xong việc. APP KHỞI ĐỘNG APP TẮT THỜI GIAN → Singleton sống tới lúc app tắt Scoped bị bắt giữ captive dependency Scoped đúng cách 1 scope cho 1 request Bản sửa CreateAsyncScope mỗi lần GIỮ TRONG FIELD CustomerCacheRefresher · resolve đúng 1 lần CrmDbContext · change tracker phình, dữ liệu cũ, race condition Request A · DbContext riêng dispose Request B · DbContext riêng dispose Request C · DbContext riêng dispose scope dispose scope dispose scope dispose CHÚ GIẢI Instance đang sống Instance bị bắt giữ Container dispose instance © tiennhm