gpt4 book ai didi

c# - 如何确定是否在 Windows 服务内部启动?

转载 作者:太空狗 更新时间:2023-10-29 22:15:11 25 4
gpt4 key购买 nike

目前我正在通过以下方式检查它:

if (Environment.UserInteractive)
Application.Run(new ServiceControllerForm(service));
else
ServiceBase.Run(windowsService);

它有助于调试一点,服务也可以使用可执行文件运行。但现在假设该服务需要与用户桌面交互,因此我必须在属性中启用“允许服务与桌面交互”。这当然打破了这种检查方式。还有别的办法吗?

最佳答案

此外,必须注意,Environment.UserInteractive.NET Core 中始终返回 true,即使它作为Windows 服务。

目前,最好的方法似乎是 this one from ASP.NET Core .

资料来源: .NET Core 2.2 .NET Core 3.1

Fixed in .NET 5

关于c# - 如何确定是否在 Windows 服务内部启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2397162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com