[英]ASP.Net Core: Disposing of SSH client
I am using SSH.NET to connect to a database via ssh tunnel in an ASP.Net Core application. 我正在使用SSH.NET通过ASP.Net Core应用程序中的ssh隧道连接到数据库。 I define a query session in the Startup.cs via dependency injection like this: 我通过依赖项注入在Startup.cs中定义查询会话,如下所示:
services.AddScoped<IQuerySession>(ds =>
{
var store = ds.GetRequiredService<IDocumentStore>();
using (var client = new SshClient("99.99.99.99", "admin", "test"))
{
client.Connect();
return store.QuerySession();
}
});
But how do I dispose of the ssh client when the scoped service is disposed? 但是,当处置范围服务时,如何处置ssh客户端?
SshClient implements IDisposable so the .net framework will call Dispose() when the service is finished being used. SshClient实现IDisposable,因此当服务使用完毕时,.net框架将调用Dispose()。
Good news, you don't have to do anything. 好消息,您无需执行任何操作。
See here for more detail: How to free resources and dispose injected service in ASP.NET 5/Core by the end of request? 请参阅此处以获取更多详细信息: 如何在请求结束时释放资源并在ASP.NET 5 / Core中处置注入的服务?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.