.Net Core 依賴注入

@zgcwkj  2023年06月29日

分類:

代碼 網站 

.Net Core 依賴注入幾個函數(記錄一下,怕忘記~)

函數

//每個請求,都獲取一個新的實例
AddTransient

//同一請求,都獲取到相同的實例
AddScoped

//每次都獲取同一個實例
AddSingleton

示例

//不參數
services.AddTransient<T>();
services.AddScoped<T>();
services.AddSingleton<T>();

//帶參數
services.AddTransient(x => new T(data));
services.AddScoped(x => new T(data));
services.AddSingleton(x => new T(data));


評論已關閉

Top