The service itself is designed so that it runs several threads. Unhandled exceptions cause it to crash. Tried: `Application.ThreadException + = Application_ThreadException; ' did not take (mb. because errors fell in another thread) This is: 'AppDomain.CurrentDomain.UnhandledException + = CurrentDomain_UnhandledException;' It helped more, at least I was able to push the error into the log. But the service has stopped.

How to make her work is not interrupted, and the error was processed and recorded in the log?

  • one
    Catch the error in those threads where it occurs. - Pavel Mayorov
  • @PavelMayorov is the question - the service should work even with unhandled exceptions. - kvvk
  • one
    Then someone has to start threads again. Because at the time of the event, "something fell in the domain," the stream was already dead and could not be restored. - Monk
  • The service can be restarted by the system .... In the service settings, click on it twice and on the Recovery tab specify what needs to be done with it after the first, second and all other breakdowns, surely these configs can also be set from the .net environment ... - kimaman2
  • one
    @kvvk, and you work with streams? There is a Task in which everything is done more easily. - iluxa1810

1 answer 1

I hardly think of a service that continues with its unhandled exceptions.

For example, there is a service that receives something from the user and writes it to the database.

Imagine that a record in the database is an unhandled exception (for example, an empty catch , so that the application does not fall).

It turns out that the service continues to work, and in this case, the user gets the impression that everything is fine, but in fact his data is not written to the database.

Conclusion: Provide for all exceptions and their processing, otherwise the appearance of correct work will be created, and in fact there will be unpredictable behavior. If you really do not expect this behavior, then let the service drop with a log entry. Analyzing the log, you will get a place of occurrence of an error, where you can foresee a new place for handling an exception, if possible.

Sense to keep the service active if an unhandled exception has occurred? After all, the performance of the service can go on unpredictable ways, up to infinite cycles ...


If the service is multifunctional, then you can store data about active functions somewhere.

If an unhandled exception occurred in a thread that implements a function (that killed the thread, then we make this function inaccessible.

When calling a particular function, look for it in the collection of functions and check that it can be used.

It turns out that the service works correctly, but some of the functionality does not work.

  • one
    “Provide for all exceptions and their processing, otherwise it will appear to work correctly, and in fact there will be unpredictable behavior.” I fully approve! - VladD
  • Thanks, the task didn’t really envisage a service that works by all means - kvvk