AddSingleton()方法創建一個Singleton服務,首次請求會創建服務,然后,所有后續的請求中都會使用相同的實例,整個應用程序生命周期都使用該單個實例
AddScoped():不同http清求,實例不同,同名謂詞不同,也不行。例如httpget跟httppost,作用域是一定范圍內,例如從同一個post請求的create方法,只能統計一次,每次請求都是新的實例
AddTransient():臨時服務,每次請求時,都會創建一個新的Transient服務實例
使用例子:
Startup.cs里:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IStudentRepository, MokeStudentRepository>();//services.方法
}
