-1

I have a console application which I want to use to run as Windows Service. When I run the application manually I see the console window and there is an error, so the application is immediately closed. How can I keep my console app window up when error occur?

Here's my code:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
             Host.CreateDefaultBuilder(args)
                 .ConfigureWebHostDefaults(webBuilder =>
                 {
                     webBuilder.UseStartup<Startup>()
                         .UseUrls("http://0.0.0.0:5678", "https://0.0.0.0:5678");
                 }).UseWindowsService();
    }

I've tried to add Console.ReadLine(); just after the line but doesn't work as expected. Once after error occurs application is closed.

CreateHostBuilder(args).Build().Run();
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Henry
  • 537
  • 1
  • 9
  • 22

1 Answers1

0

You can put your code into a try catch block to return the error to your console.

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
          CreateHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            Console.ReadKey();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
         Host.CreateDefaultBuilder(args)
             .ConfigureWebHostDefaults(webBuilder =>
             {
                 webBuilder.UseStartup<Startup>()
                     .UseUrls("http://0.0.0.0:5678", "https://0.0.0.0:5678");
             }).UseWindowsService();
}
Saiko
  • 102
  • 6