在ASP.NET Core应用程序关闭时退出Firefox Selenium Webdriver

时间:2018-10-18 19:11:17

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

在ASP.NET Core MVC 2.1上使用Selenium,我有一个由DI注入的类。它创建一个FirefoxDriver实例,当基础ASP.NET Core应用程序终止时,该实例应退出(关闭浏览器窗口并退出过程)。

public class FFManager:IDisposable {
        RemoteWebDriver driver;
        public FFManager () {
            driver = new FirefoxDriver();
        }
        public void Dispose() {
            driver.CloseDriver();
        }
}

Startup.ConfigureServices

services.AddSingleton<FFManager>(x => new FFManager());

不起作用

如您所见,我实现了IDisposable接口。进程退出后,Firefox窗口仍然打开。另一个想法是在applicationLifetime.ApplicationStopping

中使用Startup.Configure事件
let manager = app.ApplicationServices.GetService<FFManager>();
manager.Dispose();

相同的结果。在第二种方法中,事件肯定已命中(已测试断点/控制台输出),并且ASP.NET Core应用程序的关闭过程要慢一些。但是,退出该应用程序后,Firefox仍处于打开状态。为什么?

当我将服务的生存期更改为作用域时,Firefox实例将在请求后关闭。但这应在应用退出后而不是在每次请求后发生。我没有任何错误,应用程序顺利关闭。

0 个答案:

没有答案