本文介绍了何时处置.NET Core依赖项注入的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! ASP.NET Core在 IServiceCollection 上使用扩展方法来设置依赖项注入,然后在需要一种类型时,它使用适当的方法来创建新实例:ASP.NET Core uses extension methods on IServiceCollection to set up dependency injection, then when a type is needed it uses the appropriate method to create a new instance: AddTransient< T> -添加每次请求时都会再次创建的类型。 AddScoped< T> -添加用于请求范围的类型。 AddSingleton< T> -在首次请求时添加类型并保留它。AddTransient<T> - adds a type that is created again each time it's requested.AddScoped<T> - adds a type that is kept for the scope of the request.AddSingleton<T> - adds a type when it's first requested and keeps hold of it.我具有实现 IDisposable 的类型,如果不处理它们将导致问题-在每个模式中,当处置实际被调用吗?I have types that implement IDisposable and that will cause problems if they aren't disposed - in each of those patterns when is Dispose actually called?是否需要添加任何内容(例如异常处理)以确保实例始终被处置?Is there anything I need to add (such as exception handling) to ensure that the instance is always disposed?推荐答案已解析的对象与其容器具有相同的生命周期/处置周期,确保您使用语句或 .Dispose()方法手动使用代码处理临时服务。The resolved objects have the same life-time/dispose cycle as their container, that's unless you manually dispose the transient services in code using using statement or .Dispose() method.在ASP.NET Core中,您将获得一个范围限定的容器,该容器会针对每个请求进行实例化,并在请求结束时进行处理。此时,此容器创建的作用域和临时依赖关系也将被释放(即如果它们实现 IDisposable 接口),您也可以在源代码上看到此处。In ASP.NET Core you get a scoped container that's instantiated per request and gets disposed at the end of the request. At this time, scoped and transient dependencies that were created by this container will get disposed too (that's if they implement IDisposable interface), which you can also see on the source code here.public void Dispose(){ lock (ResolvedServices) { if (_disposeCalled) { return; } _disposeCalled = true; if (_transientDisposables != null) { foreach (var disposable in _transientDisposables) { disposable.Dispose(); } _transientDisposables.Clear(); } // PERF: We've enumerating the dictionary so that we don't allocate to enumerate. // .Values allocates a ValueCollection on the heap, enumerating the dictionary allocates // a struct enumerator foreach (var entry in ResolvedServices) { (entry.Value as IDisposable)?.Dispose(); } ResolvedServices.Clear(); }}通常在处理父容器时处理子句表示应用程序何时关闭。Singletons get disposed when the parent container gets disposed, usually means when the application shuts down. TL; DR :只要在应用程序启动期间不实例化作用域/瞬态服务(使用 app .ApplicationServices.GetService< T>()),并且您的服务正确实现了Disposable接口(例如在MSDN中指向),您无需担心。TL;DR: As long as you don't instantiate scoped/transient services during application startup (using app.ApplicationServices.GetService<T>()) and your services correctly implement Disposable interface (like pointed in MSDN) there is nothing you need to take care of.除非您做一些时髦的事情,否则在 Configure(IApplicationBuilder app)方法之外,父容器不可用。外部可访问(无论如何都不应该)。The parent container is unavailable outside of Configure(IApplicationBuilder app) method unless you do some funky things to make it accessible outside (which you shouldn't anyways).当然,它鼓励尽快释放临时服务,尤其是在消耗大量资源的情况下。Of course, its encouraged to free up transient services as soon as possible, especially if they consume much resources. 这篇关于何时处置.NET Core依赖项注入的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!