• Dependency Injection in ASP.NET Core


    Transient – A new instance of the service is created each time it is requested. It can be used for stateless and light weight services.

    可以理解为每次都要创建,主要针对状态无关、轻量级的服务。

    Scoped – A single instance is created once per request.

    每次HttpRequest就创建一次,HttpRequest以内就不用创建了;下一次HttpRequest的话要重新创建。

    Singleton – Created only once the first time they are requested.

    应用程序内只创建一次。

    玩过Autofac的同学发现这个其实和Autofac是一样的。

    Ref:http://www.c-sharpcorner.com/article/dependency-injection-in-Asp-Net-core/

    Startup的ConfigureServices主要用于依赖注入的配置

    public void ConfigureServices(IServiceCollection services)
            {
                // Add framework services.
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
                services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    
                services.AddMvc();
    
                // Add application services.
                services.AddTransient<IEmailSender, AuthMessageSender>();
                services.AddTransient<ISmsSender, AuthMessageSender>();
    
                services.AddTransient<FruitServices>();
            }
    
  • 相关阅读:
    机器学习---14
    机器学习---13
    机器学习--10
    机器学习--9
    机器学习--8
    机器学习--7
    机械学习--6
    大数据应用技术课程实践--选题与实践方案
    15.手写数字识别-小数据集
    14.深度学习-卷积
  • 原文地址:https://www.cnblogs.com/pengzhen/p/5756507.html
Copyright © 2020-2023  润新知