【问题标题】:ASP.Net Core: Disposing of SSH clientASP.Net Core:处理 SSH 客户端
【发布时间】:2019-05-18 01:28:05
【问题描述】:

我正在使用 SSH.NET 通过 ASP.Net Core 应用程序中的 ssh 隧道连接到数据库。 我通过依赖注入在 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();
    }
});

但是当作用域服务被释放时,我该如何释放 ssh 客户端呢?

【问题讨论】:

    标签: c# asp.net-core ssh dependency-injection


    【解决方案1】:

    SshClient 实现了 IDisposable,因此 .net 框架将在服务使用完毕时调用 Dispose()。

    好消息,您无需执行任何操作。

    更多详情请看这里:How to free resources and dispose injected service in ASP.NET 5/Core by the end of request?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多