2

I am working on an application that will run in Kubernetes. Kubernetes relies on the application to know if it is healthy or not.

So, I need to know when I get a critical exception thrown. By "Critical", I mean Out of Memory, Stack Overflow, etc. Things that mean that the container should be killed.

I have seen things in ASP.Net Core that allow you to show an error page when an exception happens, but I need this to happen with both UI and Web API applications. And I don't really want it to interact with my UI at all (on the ones that have a UI ).

Is there an event (or something similar) that is raised when an exception was thrown in an ASP.Net Core application?

3
  • 1
    You cannot catch OutOfMemoryException or StackOverflowException at all in .NET. They just kill your program on the spot, even if you have error handling in place. Commented Aug 10, 2021 at 18:30
  • @Alejandro - I think you might be wrong in the OutOfMemoryException. I have a ASP.Net Core Web API app running in a container that I setup to have an operation that consumes memory. The operation takes a few calls to consume all the memory. After I make the few calls, it throws the OutOfMemoryException. But the other (low-memory) operations keep working fine. But a check shows that the container did not die, and is continuing to hold onto the memory. (And another call throws another OutOfMemoryException.) Commented Aug 10, 2021 at 18:40
  • Alejandro is definitely right about StackOverflowException. But I agree with Vaccano I have been able to handle OutOfMemoryException's at least to the point of logging it. Commented Aug 11, 2021 at 0:32

1 Answer 1

3

A .NET application will not be able to handle “critical” issues like memory issues or stack overflows in a way that it can report about its own health. There are basically two possible outcomes with unexpected errors: The application can handle the problem, in which case the ASP.NET Core application is expected to work properly for future requests, or the process terminates abruptly.

Observing the latter should be done from the outside. You can do this for example by checking if the process is still alive in your container.

Another option would be to employ health checks which is a way for an ASP.NET Core application to report about its own health:

Health probes can be used by container orchestrators and load balancers to check an app's status. For example, a container orchestrator may respond to a failing health check by halting a rolling deployment or restarting a container. A load balancer might react to an unhealthy app by routing traffic away from the failing instance to a healthy instance.

So your container orchestrator could check whether the ASP.NET Core application is still able to respond to a health probe, and if it isn’t assume that the application crashed in some way or another, requiring a container restart.

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to setup health checks. I thought an Out of Memory Exception would cause the process to die, but it did not. (The operation failed, but the process is still running, responding to other low-memory operations, and still holding onto all the memory.) So I need someway to know it happened so I can tell the health check to return unhealthy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.