how to hide kestrel console? [duplicate]
Asked Answered
I

1

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

Ias answered 19/7, 2017 at 20:25 Comment(2)
Is that not just debugging?Grove
nope. I added the Program.Main code, and it shows the console also when I build in release mode, and running the .exe fileIas
I
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
Ias answered 20/7, 2017 at 11:34 Comment(7)
Or just host an aspnet core app in a Windows ServiceMaster
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 appIas
@YuvalPerelman Could you explain this a bit more?Prismatic
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.Ias
The solution now seems to be far simpler with .net core 3.0+: > Simply set <OutputType>WinExe</OutputType> in csprojHealthful
@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.Amii
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: #1473133Ias

© 2022 - 2024 — McMap. All rights reserved.