5

I have a .net core app that I want to run in background, but I can't seem to get rid of the console window of Kestrel. Is there any way to hide it without running the app as a windows service? I've tried to remove any reference related to Logger, it didnt help.
here is my 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();

Thanks

Yuval Perelman
  • 4,499
  • 1
  • 22
  • 32

1 Answers1

6

the solution was using editbin.exe as described here https://github.com/AvaloniaUI/Avalonia/wiki/Hide-console-window-for-self-contained-.NET-Core-application

editbin.exe /subsystem:windows yourapp.exe
Yuval Perelman
  • 4,499
  • 1
  • 22
  • 32
  • Or just [host an aspnet core app in a Windows Service](https://learn.microsoft.com/en-us/aspnet/core/hosting/windows-service) – QuantumHive Aug 02 '17 at 08:06
  • 2
    Windows service is an entirely different use case with a lot of constraints and headaches of it's own. No reason to go there when all I need is a simple windows app – Yuval Perelman Aug 02 '17 at 08:54
  • 1
    @YuvalPerelman Could you explain this a bit more? – Pomster Apr 20 '18 at 09:45
  • 2
    Windows service runs from a different security context than the current user. It is a headache to install and uninstall. It has no access to anything that has GUI and it can't open other processes with GUI. It usually runs under admin context, so it has no access to mapped drives or stored credentials for network locations, and many other UAC related headaches. You cant have 2 instances of it running in parallel for different logged on users. It doesn't shut down when the user logs out. That's only from the top of my mind. A service and a local process are not for the same use cases. – Yuval Perelman Apr 22 '18 at 10:35
  • 1
    The solution now seems to be far simpler with .net core 3.0+: > Simply set WinExe in csproj – Vadim Peretokin May 11 '21 at 09:40
  • @YuvalPerelman thanks , this is what I needed . one more thing , any idea how I can stop this application , after hiding its cmd ,using any UI element or button or any windows icon. currently I have to stop the application from task manager. – Dragon May 16 '21 at 19:34
  • when I had to do that I wrapped the .net core app with a win forms application that started the .net core app on start and killed it on shut down. I've added to that app a notify icon with all the functionality I needed. Here is an example as to how to add the notify icon: https://stackoverflow.com/questions/1472633/wpf-application-that-only-has-a-tray-icon – Yuval Perelman May 17 '21 at 17:25