Windows service for after Exception

Asked

Viewed 89 times

2

Is there any way to make a Windows service created on don’t give Stop() after a Exception?

The service was created with a timer, which will run every 1 hour, but there was an error in the execution and returned a exception, until then all right, the problem is that when the time came, the timer did not start.

Need to do some specific treatment on Catch?

The section below is called when the Timer is executed.

the time is another Timer

private void ExecutarServico()
{
    timerImportacao.Enabled = false;
    CarregarConfiguracoesServico();
    CadastroBusiness biz = new CadastroBusiness();
    try
    {
        if (GeraArquivosExportacao())
        {
            AlertaBusiness.RegistrarLogEventViewer("FEPWeb - Iniciando processo de Exportação.");

            ServicoFepWeb sfw = new ServicoFepWeb();
            sfw.UriInput = uriInput; sfw.DirInput = operServico.CaminhoSaida; sfw.UserCred = userCred; sfw.PassCred = passCred;
            biz.GerarDocumentoFepWeb(operServico.CaminhoSaida);
            sfw.SendFiles();

            if (biz.RetornoFepWeb.Count != 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string str in biz.RetornoFepWeb.ToArray())
                    sb.Append(str + Environment.NewLine);
                biz.EnviarAlertaFepWebErro("Exportação FEPWeb.", MensagemBusiness.RetornaMensagens("Exportar_FepWeb_Aviso") + Environment.NewLine + sb.ToString(), operServico.DestinatarioMail);
            }
            AlertaBusiness.RegistrarLogEventViewer("Processo de Exportação FEPWeb, concluido com sucesso.");
        }
    }
    catch (Exception ex)
    {
        AlertaBusiness.RegistrarLogEventViewer(MensagemBusiness.RetornaMensagens("Exportar_FepWeb_ERR") + Environment.NewLine + ex.Message + Environment.NewLine + (ex.InnerException != null ? ex.InnerException.ToString() : String.Empty), EventLogEntryType.Error);
        biz.EnviarAlertaFepWebErro("Exportação FEPWeb.", MensagemBusiness.RetornaMensagens("Exportar_FepWeb_ERR") + Environment.NewLine + ex.Message + Environment.NewLine + (ex.InnerException != null ? ex.InnerException.ToString() : String.Empty), operServico.DestinatarioMail);
    }
    finally
    {
        timerImportacao.Enabled = true;
    }
}
  • Did not restart because it was still running? I have a service, with Topshelfthat does not stop after Exception. I was curious how this service with Timer, if the service stops, as the Timer will shoot since it is part of the service?

  • I treated Exception with Throw new Exception(), returning the failure to the initial method, so I got the error from the first point. In this case, the execution at that time is interrupted, but is not resumed in the new cycle

1 answer

0

Try putting a try {} within your catch, the error may be being caused there.

private void ExecutarServico()
{
    timerImportacao.Enabled = false;
    CarregarConfiguracoesServico();
    CadastroBusiness biz = new CadastroBusiness();
    try
    {
        if (GeraArquivosExportacao())
        {
            AlertaBusiness.RegistrarLogEventViewer("FEPWeb - Iniciando processo de Exportação.");

            ServicoFepWeb sfw = new ServicoFepWeb();
            sfw.UriInput = uriInput; sfw.DirInput = operServico.CaminhoSaida; sfw.UserCred = userCred; sfw.PassCred = passCred;
            biz.GerarDocumentoFepWeb(operServico.CaminhoSaida);
            sfw.SendFiles();

            if (biz.RetornoFepWeb.Count != 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string str in biz.RetornoFepWeb.ToArray())
                    sb.Append(str + Environment.NewLine);
                biz.EnviarAlertaFepWebErro("Exportação FEPWeb.", MensagemBusiness.RetornaMensagens("Exportar_FepWeb_Aviso") + Environment.NewLine + sb.ToString(), operServico.DestinatarioMail);
            }
            AlertaBusiness.RegistrarLogEventViewer("Processo de Exportação FEPWeb, concluido com sucesso.");
        }
    }
    catch (Exception ex)
    {
        try {
            AlertaBusiness.RegistrarLogEventViewer(MensagemBusiness.RetornaMensagens("Exportar_FepWeb_ERR") + Environment.NewLine + ex.Message + Environment.NewLine + (ex.InnerException != null ? ex.InnerException.ToString() : String.Empty), EventLogEntryType.Error);
        biz.EnviarAlertaFepWebErro("Exportação FEPWeb.", MensagemBusiness.RetornaMensagens("Exportar_FepWeb_ERR") + Environment.NewLine + ex.Message + Environment.NewLine + (ex.InnerException != null ? ex.InnerException.ToString() : String.Empty), operServico.DestinatarioMail);
        } catch {
            ;
        }
    }
    finally
    {
        timerImportacao.Enabled = true;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.