AddSingleton()方法创建一个Singleton服务,首次请求会创建服务,然后,所有后续的请求中都会使用相同的实例,整个应用程序生命周期都使用该单个实例

AddScoped():不同http清求,实例不同,同名谓词不同,也不行。例如httpget跟httppost,作用域是一定范围内,例如从同一个post请求的create方法,只能统计一次,每次请求都是新的实例

AddTransient():临时服务,每次请求时,都会创建一个新的Transient服务实例

 

 

使用例子:

Startup.cs里:

 

  public void ConfigureServices(IServiceCollection services)
      {
      
            services.AddTransient<IStudentRepository, MokeStudentRepository>();//services.方法
           
        }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
  • 2021-08-15
  • 2021-06-04
  • 2022-02-19
  • 2022-12-23
猜你喜欢
  • 2021-05-29
  • 2022-12-23
  • 2021-04-11
  • 2021-12-10
  • 2021-11-14
相关资源
相似解决方案