C# - Application Windows Service Crash

0

I have a project in C# built in Windows Service. It works fine but it crash sometimes without any reason.

When i check Windows Event Viewer, the only thing i can see is : Application crash Log

Nom de l’application défaillante <<MyApplicationName>>, version : 7.3.0.0, horodatage : 0x5f4ca7db
Nom du module défaillant : bcryptPrimitives.dll, version : 6.3.9600.18895, horodatage : 0x5a4b0740
Code d’exception : 0xc00000fd
Décalage d’erreur : 0x00001c1d
ID du processus défaillant : 0x1330
Heure de début de l’application défaillante : 0x01d67fc7fd46a198
Chemin d’accès de l’application défaillante : <<MyApplicationPath>>
Chemin d’accès du module défaillant: C:\Windows\SYSTEM32\bcryptPrimitives.dll
ID de rapport : a4242415-ec19-11ea-80ea-00155d0dc716
Nom complet du package défaillant : 
ID de l’application relative au package défaillant : 

I have check the exception code 0xc00000fd and it seems to be a Stack Overflow error. But i can't find where it crash, and i can't start the project in debug mode because it can crash in 3 days or in an hour.

What i have tried :

            try
            {
                
                ServiceBase[] ServicesToRun;
                Service service = new Service();
                ServicesToRun = new ServiceBase[]
                {
                    service
                };
                if (Process.GetCurrentProcess().ProcessName.Contains("vshost") || RUN_DEBUG)
                {
                    service.Start();
                    while (true) //Code to execute in debug mode, but not the problem
                    {
                        Task.Delay(1000);
                    }
                }
                else
                {                
                    ServicesToRun[0].AutoLog = true;
                    ServiceBase.Run(ServicesToRun);
                }
            }
            catch(Exception ex)
            {
                DateTime now = DateTime.Now;
                string date = now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second;

                string stackTrace = "StackTrace-" + date + ".txt";
                string message = "ExceptionMessage-" + date + ".txt";
                string innerExceptionStackTrace = "InnerStackTrace-" + date + ".txt";
                string innerExceptionMessage = "InnerMessage-" + date + ".txt";

                if (!Directory.Exists("CrashReport-" + date))
                    Directory.CreateDirectory("CrashReport-" + date);
                System.IO.File.Create("CrashReport-" + date + "\\" + stackTrace).Close();
                System.IO.File.Create("CrashReport-" + date + "\\" + message).Close();

                File.AppendAllText("CrashReport-" + date + "\\" + stackTrace, ex.StackTrace);
                File.AppendAllText("CrashReport-" + date + "\\" + message, ex.Message);

                if (ex.InnerException != null)
                {
                    System.IO.File.Create("CrashReport-" + date + "\\" + innerExceptionStackTrace);
                    System.IO.File.Create("CrashReport-" + date + "\\" + innerExceptionMessage);

                    File.AppendAllText("CrashReport-" + date + "\\" + innerExceptionStackTrace, ex.InnerException?.StackTrace);
                    File.AppendAllText("CrashReport-" + date + "\\" + innerExceptionMessage, ex.InnerException?.Message);
                }
                 

                throw ex;
            }

My objective was to catch the error at the highest level of my application. But it didn't work.

I just make the service restart automaticaly when it crash, but i want to solve the bug.

Thank you for your help and sorry for my bad English.

UPDATE :

Moved the try catch section into the service. Service crashed again without report.

Executed the service in debug mode, no crash. By the way, the service in normal execution crashed without any call on it.

Crash happen every ~9h. Too long for a normal stack overflow. I have no recursion in my code. The only thing i got in service code is a While with a Thread.Sleep for waiting new treatment

The service in debug mode is currently used, but it doesn't crash. Is it a Service problem ? Other services (C# too) running on this computer without any problem.

c#
.net
exception
service
stack-overflow
asked on Stack Overflow Sep 1, 2020 by Noz • edited Sep 2, 2020 by Noz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0