【问题标题】:how to hide kestrel console? [duplicate]如何隐藏红隼控制台? [复制]
【发布时间】:2017-07-19 20:25:58
【问题描述】:

我有一个想要在后台运行的 .net 核心应用程序,但我似乎无法摆脱 Kestrel 的控制台窗口。有没有办法在不将应用程序作为 Windows 服务运行的情况下隐藏它?我已尝试删除与 Logger 相关的任何参考,但没有帮助。
这是我的 Program.Main:

            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("hosting.json", optional: true)
                .Build();
            var hostingUrl = config.GetValue<string>("HostingUrl");
            if (string.IsNullOrEmpty(hostingUrl))
            {
                var xmlString = File.ReadAllText(Consts.WebHostBaseFolder + "\\web.config");
                var confDoc = XDocument.Parse(xmlString);
                hostingUrl = confDoc.Element("configuration").Element("appSettings")?.Elements("add")?.FirstOrDefault(e => e.Attribute("key").Value == "HostingUrl")?.Attribute("value")?.Value ?? "";

            }
            var host = new WebHostBuilder()
                            .UseKestrel()
                            .UseContentRoot(Consts.WebHostBaseFolder)
                            .UseStartup<Startup>()
                            .UseUrls(hostingUrl)
                            .Build();

                host.Run();

谢谢

【问题讨论】:

  • 那不只是调试吗?
  • 不。我添加了 Program.Main 代码,当我在发布模式下构建并运行 .exe 文件时,它也会显示控制台

标签: c# asp.net-core-mvc .net-core kestrel


【解决方案1】:

解决方案是使用editbin.exe,如此处所述https://github.com/AvaloniaUI/Avalonia/wiki/Hide-console-window-for-self-contained-.NET-Core-application

editbin.exe /subsystem:windows yourapp.exe

【讨论】:

  • Windows 服务是一个完全不同的用例,它有很多限制和令人头疼的问题。当我只需要一个简单的 Windows 应用程序时,没有理由去那里
  • @YuvalPerelman 你能再解释一下吗?
  • Windows 服务从与当前用户不同的安全上下文运行。安装和卸载很头疼。它无法访问任何具有 GUI 的东西,也无法使用 GUI 打开其他进程。它通常在管理员上下文下运行,因此它无法访问映射的驱动器或存储的网络位置凭据,以及许多其他与 UAC 相关的问题。您不能为不同的登录用户并行运行 2 个实例。用户注销时它不会关闭。这只是我的想法。服务和本地进程不适用于相同的用例。
  • 使用 .net core 3.0+ 的解决方案现在似乎要简单得多: > 只需在 csproj 中设置 WinExe
猜你喜欢
  • 2011-02-08
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2011-10-22
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多