Dependency Injection in ASP.NET Core
2016-08-10 14:08
896 查看
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主要用于依赖注入的配置
可以理解为每次都要创建,主要针对状态无关、轻量级的服务。
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 configurations into views in asp.net core
- Dependency Injection in ASP.NET MVC
- Dependency Injection in ASP.NET Web API 2
- Dependency injection in .NET Core的最佳实践
- Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)
- Asp.net core下用Dependency Injection分离业务处理
- ASP.NET Core 源码阅读笔记(1) ---Microsoft.Extensions.DependencyInjection
- Dependency Injection and Inversion of Control with ASP.NET MVC
- How to : Protect Form Sql Injection in asp.net
- ASP.NET 5 Dependency Injection with Autofac
- [转]Session and application state in ASP.NET Core
- [转]How to override HandleUnauthorizedRequest in ASP.NET Core
- Introducing ASP.NET Core: The New ASP.NET in Town!
- WebSocket in ASP.NET Core
- Adding Cache-Control headers to Static Files in ASP.NET Core
- Database Change Notifications in ASP.NET using SignalR and SqlDependency
- [转]How do you create a custom AuthorizeAttribute in ASP.NET Core?
- Bower In ASP.NET Core
- [译]Writing Custom Middleware in ASP.NET Core 1.0
- Implementing Policy Injection in ASP.NET Applications