拙网论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 202|回复: 0

.NET Core和ASP.NET Core及WEB开发知识点

[复制链接]

949

主题

1001

帖子

3736

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3736
发表于 2019-8-30 10:23:44 | 显示全部楼层 |阅读模式
Dependency injection into views in ASP.NET Core

Dependency injection in ASP.NET Core

Architectural principles

Write custom ASP.NET Core middleware

Service lifetimes
Choose an appropriate lifetime for each registered service. ASP.NET Core services can be configured with the following lifetimes:
Transient
Transient lifetime services (AddTransient) are created each time they're requested from the service container. This lifetime works best for lightweight, stateless services.
Scoped
Scoped lifetime services (AddScoped) are created once per client request (connection).
Warning
When using a scoped service in a middleware, inject the service into the Invoke or InvokeAsyncmethod. Don't inject via constructor injection because it forces the service to behave like a singleton. For more information, see Write custom ASP.NET Core middleware.

Singleton
Singleton lifetime services (AddSingleton) are created the first time they're requested (or when Startup.ConfigureServices is run and an instance is specified with the service registration). Every subsequent request uses the same instance. If the app requires singleton behavior, allowing the service container to manage the service's lifetime is recommended. Don't implement the singleton design pattern and provide user code to manage the object's lifetime in the class.
Warning
It's dangerous to resolve a scoped service from a singleton. It may cause the service to have incorrect state when processing subsequent requests.


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|抱朴守拙BBS

GMT+8, 2025-5-25 21:28 , Processed in 0.216747 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表