Stack Overflow Asked by suman_dh on November 20, 2021
I have used the following code section in a windows service application to handle unhandled exception.
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
private static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomainUnhandledExceptionHandler);
if (args.FirstOrDefault(s => s.ToLower() == "console") != null)
{
ConsoleMain(args);
}
else
{
ServiceMain(args);
}
}
private static void CurrentDomainUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
try
{
//Code before service stops
}
catch (Exception ex)
{
}
}
protected override void OnStart(string[] args)
{
var applicationThread = new Thread(Run) {IsBackground = true};
applicationThread.Start();
}
This code works for all unhandled exception while in debug mode in console. But it doesn’t work when I run the application as windows service. It doesn’t hit the handler function but the service stops with error(exception) as seen in event log. What might be the problem?
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP