简体   繁体   English

如何在.NET Core Console应用程序中处理Scoped服务实例?

[英]How is a Scoped service instance handled in a .NET Core Console application?

I copied this from ConfigureServices in a web application I'm building, where I'm trying to move away from the web and only use a console app or service: 我在我正在构建的Web应用程序中从ConfigureServices复制了这个,我试图远离Web,只使用控制台应用程序或服务:

serviceCollection.AddScoped<IDbConnection, SqlConnection>(c => new SqlConnection(App.Configuration.GetConnectionString("DefaultConnection")));

The console app works fine, but I'm wondering how the lifetime of the connection is handled. 控制台应用程序工作正常,但我想知道如何处理连接的生命周期。 If and when is the connection closed and or disposed? 连接是否以及何时关闭或处置? Or does this behave the same as a transient instance, and I should dispose it myself? 或者这与瞬态实例的行为相同,我应该自己处理它?

When you build IServiceProvider from IServiceCollection ( BuildServiceProvider method) and you use this instance of IServiceProvider to resolve IDbConnection you will get same instance of IDbConnection every time. 当您从IServiceCollectionBuildServiceProvider方法)构建IServiceProvider并使用此IServiceProvider实例来解析IDbConnection ,每次都会获得相同的IDbConnection实例。 Scope is connected to IServiceProvider . Scope连接到IServiceProvider to create new scope you need to resolve from the container IServiceScopeFactory and use it to create IServiceProvider that is scoped: 要创建新的作用域,您需要从容器IServiceScopeFactory解析并使用它来创建作用域的IServiceProvider

using (var scope = scopeFactory.CreateScope())
{
   var scopedConnection = scope.ServiceProvider.GetRequiredService<IDbConnection>();      
}

Connection will be disposed when scope is disposed. 处理范围时将处理连接。

In ASP Core scopes are managed for you by middleware which creates new scope and then uses IServiceProvider attached to this scope to resolve Controller and everything within this web request. 在ASP Core中,中间件为您管理范围,创建新范围,然后使用附加到此范围的IServiceProvider来解析Controller和此Web请求中的所有内容。 In console application you need to manage scopes yourself. 在控制台应用程序中,您需要自己管理范围。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 .net 核心控制台应用程序中启动范围托管服务实例 - Start start scoped hosted service instance in .net core console application 如何将范围服务注入DbContext? 网络核心 - How to inject a scoped service into DbContext? Net Core ASP.NET Core DI:如果将作用域服务注册为服务类型和实现类型,则解析相同的实例 - ASP.NET Core DI: Resolve same instance if scoped service is registered both as service type and implementation type ApplicationServices 解析网络核心中不同范围的实例? - ApplicationServices resolves a different scoped instance in net core? 如何让 EF Core 工具从控制台应用程序的服务提供者处获取 DbContext 实例? - How to make EF Core tools obtain DbContext instance from service provider of a console application? .net 核心中的范围服务 DI 和 GC - 澄清? - Scoped service DI & GC in .net CORE - Clarification? 连接到现有的 SOAP API 服务 in.Net Core 控制台应用程序 - Connect to existing SOAP API service in .Net Core console application 如何在启动时作为 raspberry pi 的(守护进程)服务执行 .NET Core 控制台应用程序 - How to execute a .NET Core console application on startup as a (daemon) service for the raspberry pi 从.net Core 2.1中的单例服务注入范围服务 - Injecting scoped service from singleton service in .net core 2.1 在asp.net核心中部署作用域服务之前如何执行方法? - How to execute method before scoped service is disposed in asp.net core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM