Restart service when generating exception

0

I have a service that receives events by TCP running, the problem is that when an exception occurs the application is blocked, what I want is that when this exception occurs, I restart the service.

using System;
using Logic;

namespace TestsConsole
{
  class Program
  {
    private static void Main(string[] args)
    {

        Console.WriteLine("Servicio Iniciado");
        ServicioBll servicioBll = new ServicioBll();

        servicioBll.Start();
        Console.Read();
        servicioBll.Stop();
    }
  }
}

This is the image of the exception.

    
asked by Sergio Andres Moreno Herrera 04.10.2017 в 02:50
source

1 answer

0

You could control the Exception and make it start again, something like being

private static void Main(string[] args)
{

    do
    {
        try
        {
            Console.WriteLine("Servicio Iniciado");
            ServicioBll servicioBll = new ServicioBll();

            servicioBll.Start();
            Console.Read();
            servicioBll.Stop();
            break;
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }while(true)
}

As you will see if there is a failure, it returns to the beginning of do..while , but if a key is pressed, it executes break then ends

    
answered by 04.10.2017 в 15:56