Service with FileSystemWatcher is not listening forever

2

I'm doing a windows service in C #, which listens to a folder and every time it has an audio file it uploads.

The problem is that it works, but after a few minutes of listening to the folder, it does not cause a problem, the service still starts, but stops working. If I simply give a stop and a star it will process again.

To try to debug I made him write a log, txt that also recorded in the windows registry (I filled in try / catch and put to register) But I can not,

Now I moved the entire project to a ClassLibrary so I can create a console program and try to debug, but nda.

My code (only some parts):

    protected override void OnStart(string[] args)
    {
          FileRepo.VerificaArquivosExistentes().ContinueWith(async result =>
           {
                var fw = new FileRepo(true);
                 await fw.FileWatch();
          });
    }
        public async Task FileWatch()
        {
            var pathAudio = ConfigurationManager.AppSettings["PathGravacao"];
            var codec = ConfigurationManager.AppSettings["Codec"];

            FileSystemWatcher watcher = new FileSystemWatcher
            {
                Path = pathAudio,
                NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName,
                Filter = codec
            };
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;

            watcher.Changed += Fsw_Changed;
//essa aqui é uma tentativa de força ele ficar na escuta para sempre
            await Task.Delay(Int32.MaxValue);
        }


   internal static void Fsw_Changed(object sender, FileSystemEventArgs e)
    {
        try
        {
            var hashBlob = Guid.NewGuid();
            var fullPath = e.FullPath;
            UploadFileAsync(e.FullPath, hashBlob).GetAwaiter().GetResult();

            DataRepo.GravaRegistroDapperAsync(fullPath, hashBlob).GetAwaiter().GetResult();

            DeletarArquivo(fullPath);
        }
        catch (Exception ex)
        {
            Log log = new Log();
            log.WriteEntry(ex);
            GravarTXT.Gravar("====> ERRO");
            GravarTXT.Gravar("          " + ex.Message);

        }
    }
    
asked by anonymous 18.12.2018 / 23:24

0 answers