2017-07-19 182 views
0

我有一个.net核心应用程序,我想在后台运行,但似乎无法摆脱Kestrel的控制台窗口。有没有办法隐藏它,而无需将应用程序作为Windows服务运行?我试图删除任何有关记录器的参考,它没有帮助。
这里是我的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(); 

感谢

+0

是不是只是调试解决方案? –

+0

nope。我添加了Program.Main代码,并且当我以发行模式构建并运行.exe文件 –

回答

1

使用editbin.exe这里描述https://github.com/AvaloniaUI/Avalonia/wiki/Hide-console-window-for-self-contained-.NET-Core-application

editbin.exe /subsystem:windows yourapp.exe 
+0

或仅在[在Windows服务中托管aspnet核心应用程序](https://docs.microsoft.com/en-us/downloads/default.aspx)时,它也显示控制台。 com/en-us/aspnet/core/hosting/windows-service) – QuantumHive

+0

Windows服务是一个完全不同的用例,它有很多限制和令人头痛的问题。当我需要的只是一个简单的Windows应用程序时,没有理由去那里 –