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>();
}

Dependency Injection in ASP.NET Core的更多相关文章

  1. Property Injection in Asp.Net Core (转载)

    问: I am trying to port an asp.net application to asp.net core. I have property injection (using ninj ...

  2. Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)

    原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...

  3. Dependency Injection in ASP.NET Web API 2

    What is Dependency Injection? A dependency is any object that another object requires. For example, ...

  4. Dependency Injection in ASP.NET Web API 2 Using Unity

    What is Dependency Injection? A dependency is any object that another object requires. For example, ...

  5. Dependency Injection in ASP.NET MVC

    原文引自http://www.dotnetcurry.com/ShowArticle.aspx?ID=786 1.传统三层结构,相邻两层之间交互: 2.如果使用EntityFramework则View ...

  6. ASP.NET Core MVC 控制器创建与依赖注入

    本文翻译自<Controller activation and dependency injection in ASP.NET Core MVC>,由于水平有限,故无法保证翻译完全准确,欢 ...

  7. DI in ASP.NET Core

    .NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...

  8. 关于单元测试的思考--Asp.Net Core单元测试最佳实践

    在我们码字过程中,单元测试是必不可少的.但在从业过程中,很多开发者却对单元测试望而却步.有些时候并不是不想写,而是常常会碰到下面这些问题,让开发者放下了码字的脚步: 这个类初始数据太麻烦,你看:new ...

  9. ASP.NET Core 2 学习笔记(四)依赖注入

    ASP.NET Core使用了大量的依赖注入(Dependency Injection, DI),把控制反转(Inversion Of Control, IoC)运用的相当巧妙.DI可算是ASP.NE ...

随机推荐

  1. Does the OpenSceneGraph have a native file format?

    From OpenSceneGraph-3.0 onwards we have new native file formats based on generic serializers that ar ...

  2. VS中新建类

    通常我们在VS中添加类,比如要声明一个car的类 我们通常在新建的时候会写成CCar,虽然新建出来的文件的名词是car,但是我们使用这个类来声明一个类的时候, 是CCar car; 如果新建类写成Ca ...

  3. hadoop 2.5 hdfs namenode –format 出错Usage: java NameNode [-backup] |

    在 cd  /home/hadoop/hadoop-2.5.2/bin 下 执行的./hdfs namenode -format 报错[hadoop@node1 bin]$ ./hdfs nameno ...

  4. hdu 2034人见人爱A-B

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2034 解题思路:set的基本用法 #include<iostream> #include& ...

  5. VGA信号一些分辨率的参数(摘抄)

    摘抄自:http://group.chinaaet.com/273/4100029440 VGA的接口时序如图所示,场同步信号VSYNC在每帧(即送一次全屏的图像)开始的时候产生一个固定宽度的高脉冲, ...

  6. json_encode() 和 json_decode()

    php json_decode返回数据js的处理,json_decode后,返回到前台的数据如:encode_str => {"green":10,"size&qu ...

  7. 在ubuntu上搭建开发环境2---Win7、Ubuntu双系统正确删除Ubuntu

    这种删除ubuntu的方式就是针对本博客中搭建双系统的方式:http://www.cnblogs.com/xumenger/p/4459963.html 如果ubuntu不是以wubi方式安装的,那么 ...

  8. [LeetCode] Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. 湘潭1247 Pair-Pair(树状数组)

    分析: 给定n个二元组,求选出两个二元组(可以是同一个)组成一序列其LIS为1,2,3,4的方法数. 分别记为s1, s2, s3, s4 s1,s4对应的情形为a >= b >= c & ...

  10. LSM-Tree (BigTable 的理论模型)(转)

    Google的BigTable架构在分布式结构化存储方面大名鼎鼎,其中的MergeDump模型在读写之间找到了一个较好的平衡点,很好的解决了web scale数据的读写问题. MergeDump的理论 ...