Search code examples
c#winformsslack-apislackserilog

How to prevent an exception which causes application crash?


I have a C# .NET 4.5.2 windows forms application which I had implemented using task and await architecture. Recently I see an strange behavior in the application. Although I have used lots of try-catch statement and almost every kind of exception is being handled some times(every 3 or 4 days) I got a X stopped working message, without any log in the application.

All I have is two entry in event viewer, first one is of type of .NET Runtime and the second one is Application Error.

For logging I use serilog which logs to Console and a rolling file. For remote communication purposes I use Slack (Which I think is the source of problem).

Here is the contents of logs:

    Application: X.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
   at SlackAPI.RequestState`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].GotResponse(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ContextAwareResult.CompleteCallback(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Net.ContextAwareResult.Complete(IntPtr)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
   at System.Net.HttpWebRequest.SetResponse(System.Exception)
   at System.Net.HttpWebRequest.SetAndOrProcessResponse(System.Object)
   at System.Net.ConnectionReturnResult.SetResponses(System.Net.ConnectionReturnResult)
   at System.Net.Connection.CompleteConnectionWrapper(System.Object, System.Object)
   at System.Net.PooledStream.ConnectionCallback(System.Object, System.Exception, System.Net.Sockets.Socket, System.Net.IPAddress)
   at System.Net.ServicePoint.ConnectSocketCallback(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ContextAwareResult.Complete(IntPtr)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

Faulting application name: X.exe, version: 1.5.0.0, time stamp: 0x5914053a
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x0655a904
Faulting process id: 0x1bb0
Faulting application start time: 0x01d2ca214de3853b
Faulting application path: PATH-TO-X.exe
Faulting module path: unknown
Report Id: 5ef2a70f-387a-11e7-9c66-000c29bbc8ee

For communicating with Slack I use SlackAPI. Any suggestion?

Update: How you can handle an exception which is not handled by threads and tasks. Is it right to put a global try catch in program.cs Main function?


Solution

  • The point is you should define the handler as soon as your app starts.

    // Bind your handler asap aap starts
    AppDomain.CurrentDomain.UnhandledException += GlobalUnhandledException;
    
    // Gereftish ;)
    static void GlobalUnhandledException(object sender, UnhandledExceptionEventArgs e) { // here we have the exception }