首页
视频
资源
登录
原
ASP.NET Core 作用域与对象释放行为 (基础)[学习笔记]
5399
人阅读
2020/5/9 13:36
总访问:
2658685
评论:
0
收藏:
0
手机
分类:
.net后台框架
![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#ASP.NET Core 作用域与对象释放行为 (基础)[学习笔记] [TOC] <br/> 知识大纲 ------------ <br/> >作用域 - IServiceScope >实现IDisposable接口类型的释放 - DI只负责释放由其创建的对象实例 - DI在容器或子容器释放时,释放由其创建的对象实例 <br/> <br/> 代码准备 ------------ <br/> >###项目结构 ![项目结构](https://img.tnblog.net/arcimg/hb/3b70e59c7a1141368af99a3d7583ec90.png "项目结构") >###项目中相关代码 <br/> ><font style="color:#2ecc71;font-weight:bold;">OrderService.cs</font> ```csharp public interface IOrderService { } public class DisposableOrderService : IOrderService, IDisposable { public void Dispose() { Console.WriteLine($"DisposableOrderService Disposed:{this.GetHashCode()}"); } } ``` ><font style="color:#2ecc71;font-weight:bold;">WeatherForecastController.cs</font> ```csharp [HttpGet] public int Get( [FromServices]IOrderService orderService, [FromServices]IOrderService orderService2, [FromServices]IHostApplicationLifetime hostApplicationLifetime, [FromQuery]bool stop = false ) { #region Console.WriteLine("=======1=========="+ " orderService :"+ orderService.GetHashCode()); using (IServiceScope scope = HttpContext.RequestServices.CreateScope()) { var service = scope.ServiceProvider.GetService<IOrderService>(); var service2 = scope.ServiceProvider.GetService<IOrderService>(); Console.WriteLine("=======2==========" + " service :" + service.GetHashCode()); Console.WriteLine("=======2==========" + " service2 :" + service2.GetHashCode()); } Console.WriteLine("=======3==========" + " orderService2 :" + orderService2.GetHashCode()); #endregion #region if (stop) { hostApplicationLifetime.StopApplication(); } #endregion Console.WriteLine("接口请求处理结束"); return 1; } ``` ><font style="color:#2ecc71;font-weight:bold;">Startup.cs</font> ```csharp public void ConfigureServices(IServiceCollection services) { services.AddTransient<IOrderService, DisposableOrderService>(); //services.AddSingleton<IOrderService>(p=>new DisposableOrderService()); //services.AddScoped<IOrderService, DisposableOrderService>(); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime) { //var s = app.ApplicationServices.GetService<IOrderService>(); //var s2 = app.ApplicationServices.GetService<IOrderService>(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } ``` <br/> Transient ------------ <br/> `services.AddTransient<IOrderService, DisposableOrderService>();` <br/> ![](https://img.tnblog.net/arcimg/hb/d285e196d2d94ca0850c61631417ed12.png) <br/> Singleton ------------ <br/> `services.AddSingleton<IOrderService>(p=>new DisposableOrderService());` <br/> ![](https://img.tnblog.net/arcimg/hb/f3e9083892f3440ca4eb4877ebc52912.png) <br/> Scoped ------------ <br/> `services.AddScoped<IOrderService, DisposableOrderService>();` <br/> ![](https://img.tnblog.net/arcimg/hb/787151664f8a4a8fb43fbcde612a66ed.png) <br/> >从上述来看可以清晰的看到对象的释放情况,这里我们可以通过stop为true时再次去观察情况 | | | | ------------ | ------------ | | `hostApplicationLifetime.StopApplication();` | 关闭当前应用程序 | <br/> 项目地址 ------------ <br/> >GitHub: https://github.com/AiDaShi/GeekTimeLearning
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
171篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
4篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
5篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术