How to prevent an exception which causes application crash? - c#

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?

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 }

Related

VS2022 + Net Maui Blazor - Start with/without debugging causes devenv.exe to crash

Running a Net Maui blazor hybrid app in Net core 7 causes VS 2022 to close without error
After reviewing event viewer there are 2 errors when the crash occurs
.Net runtime
Application: devenv.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at Microsoft.VisualStudio.Deployment.ReparsePoint.GetTarget(Microsoft.VisualStudio.Deployment.DebugLogger, System.String, Boolean ByRef)
at Microsoft.VisualStudio.Deployment.PackageManagerOperations.TranslatePackageInfo(Windows.ApplicationModel.Package)
at Microsoft.VisualStudio.Deployment.PackageManagerOperations.TranslatePackageInfoForInstalledPackages(System.Collections.Generic.IEnumerable`1<Windows.ApplicationModel.Package>, Boolean)
at Microsoft.VisualStudio.Deployment.AppxLayoutManager.CheckPackageLayoutState(Microsoft.VisualStudio.Deployment.DeployPackageName, System.String)
at Microsoft.VisualStudio.Deployment.DeploymentDriver+<ConsumeRecipeAsync>d__133.MoveNext()
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.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
Application error
Faulting application name: devenv.exe, version: 17.5.33209.295, time stamp: 0x20c46274
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x00007ff9aacefb24
Faulting process ID: 0x50a4
Faulting application start time: 0x01d92853f93d94de
Faulting application path: C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe
Faulting module path: unknown
Report ID: 783eb4ba-2b16-40ff-830a-39d2611476f1
Faulting package full name:
Faulting package-relative application ID:
I've tried to catch an error dump but with VS closing it detaches from the process before catching.
Any help would be muchly appreciated.
Had this issue occuring all day.
Not sure if it was due to me running it in administrator but, I ran the exe directly from 'C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe' opened my project and it build/ran without VS closing.

Enterprise Library 5.0 Logging exception on startup

The application is running on multiple clients. Today I had an incident, where after a power failure, the application was not starting again.
Following are the exceptions from the event log
Faulting application name: MyApplication.exe, version: 7.3.0.14, time stamp: 0x626786ff
Faulting module name: KERNELBASE.dll, version: 6.1.7601.24561, time stamp: 0x5f76968d
Exception code: 0xe0434352
Fault offset: 0x0000c5af
Faulting process id: 0xeb4
Faulting application start time: 0x01d8d17277e726c1
Faulting application path: C:\Program Files (x86)\MyCompany\Visu\MyApplication.exe
Faulting module path: C:\Windows\syswow64\KERNELBASE.dll
Report Id: b5b2baf1-3d65-11ed-b8b3-00139548ead0
Detailed exception
Application: MyApplication.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Xml.XmlException
at System.Xml.XmlTextReaderImpl.Throw(System.Exception)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Configuration.XmlUtil..ctor(System.IO.Stream, System.String, Boolean, System.Configuration.ConfigurationSchemaErrors)
at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(System.Object, System.Configuration.Internal.InternalConfigEventArgs)
Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(System.Object, System.Configuration.Internal.InternalConfigEventArgs)
at System.Configuration.Internal.InternalConfigRoot.OnConfigRemoved(System.Configuration.Internal.InternalConfigEventArgs)
at System.Configuration.Internal.InternalConfigRoot.RemoveConfigImpl(System.String, System.Configuration.BaseConfigurationRecord)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSection(System.String)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(System.String)
at System.Configuration.ConfigurationManager.GetSection(System.String)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection.GetConfigurationSourceSection()
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceFactory.Create()
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer()
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.SetCurrentContainerIfNotSet()
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.get_Current()
at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.get_Writer()
at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(System.Object, System.String, Int32, Int32)
at MyApplication.App.Main(System.String[])
Code:
[System.STAThreadAttribute()]
public static void Main(string[] args)
{
Logger.Write("Application was started on:" + DateTime.Now, "Core", 100, 100);
...
}
As I had no possiblity for remote debugger, I copied the complete application folder from the client to my dev machine. Everything was working properly. The application started and the UI was displayed.
On the client machine I tried the following
checked the application.config file - everything OK
starting the application with administrative rights - same exception
moving the folder from the "program files" directory to somewhere different, including all configuration files - same exception
comparing the current application folder with initial folder after the setup, all files are equal - same exception
As a last point, i updated the code of the main method with a try catch block around the call to the Enterprise Logger. I compiled the exe and started the updated version on the client computer. Only the exe and manifest file have been updated!
The application started and the catch block was never triggered.
I switched back to the "previously corrupted?" version and now it was successfully running.
From the text i assume that something must be wrong the the app.config file. But then it would also throw the exception on my dev machine and would still throw the exception after the update of the app.exe.
What could be the reason of the above exception?
With the tips from #PanagiotisKanavos the solution of the problem was possible.
Checking the file access with procmon, i realised that there was a user.config file stored at C:<Users><Username>\AppData\Local<MyCompany><MyApp><AppVersion>.
This file was corrupt - filled with '\0' - due to the power failure.
At startup the user.config file was merged with the app.config file and this crashed the application.

App runs directly but not after Clickonce publishing

I have written a demo application, which will be expanded later into an actual app (the demo for now is that it is only UI without any meat behind it yet). I tried to publish it, and am having problems getting the published version to run, even on my machine, where it was developed.
When I run the app under visual studio, there are no issues. Likewise, if I run it from the bin\debug or bin\release folder when studio is closed, there is also no problem.
After publishing it (more below), and running the install, it installs, but doesn't run. If I go to where the application is actually installed (c:\users]\steveb\appdata\local\apps\2.0\gibberish1\gibberish2\reallylonggibberish\tmg.exe), it runs nicely from there as well. However, from the link it makes me, it refuses to run, and in the system event viewer I get two events:
First: Net Runtime – event ID 1026
Application: applaunch.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Security.SecurityException
at TMG.UserForm..ctor()
at TMG.Program.Main()
at System.AppDomain._nExecuteAssembly(System.Reflection.RuntimeAssembly, System.String[])
at System.Runtime.Hosting.ManifestRunner.NewThreadRunner()
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.Threading.ThreadHelper.ThreadStart()
followed by:
Application error – event ID 1000
Faulting application name: applaunch.exe, version: 4.7.3056.0, time stamp: 0x5a8e5805
Faulting module name: KERNELBASE.dll, version: 10.0.17134.441, time stamp: 0x428de48c
Exception code: 0xe0434352
Fault offset: 0x000000000003a388
Faulting process id: 0x2cb0
Faulting application start time: 0x01d49dd862272806
Faulting application path: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\applaunch.exe
Faulting module path: C:\Windows\System32\KERNELBASE.dll
Report Id: cf77cf44-c3b4-44a3-b7ae-956e11898c98
Faulting package full name:
Faulting package-relative application ID:
All this is on my own machine. I have tried putting the setup on other machines, and it is not working there as well, but I didn't get to this level of debugging there, although I expect it is the same.
Any help appreciated.
akseli - your comment was correct - it was in partial trust mode, which I assume translates as "don't run this anywhere including where it was written". Thanks for the help.

WPF: Investigate WPF app crash by unhandled exceptions

I'm handling all unhandled exceptions in my WPF app using App.DispatcherUnhandledException += Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) and also all my async void methods are decorated by try-catch block, but my app still sometimes crashes and shows dialog "App has stopped working". I cannot replicate this behaviour on my developer machine, so I haven't more details about exception.
After every crash I find these 2 logs in event logger on client computers:
Application: app.exe Framework Version: v4.0.30319 Description: The
process was terminated due to an unhandled exception. Exception Info:
System.IO.IOException Stack: at
System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__5(System.Object)
at
System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(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.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch() at
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
and
app.exe 0.21.3.0 557e902a KERNELBASE.dll
6.3.9600.17415 54504ade e0434352 00014598 2770 01d0a801cc61d343
Is there any way to find source of this exception or to handle ALL exceptions on the application level?

How to see the stack trace of a deployed application that crashes on run

I have an application that crashes as soon as it is launched but only after deployment (it will run fine in debug mode).
For simplicity I've been deploying it as a CD style deployment in visual studio 2012.
This is a c# windows application.
The errors thrown (in the event log) are as follows:
Application: applaunch.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitializationException
Stack:
at Konformance.App.Main()
at System.AppDomain._nExecuteAssembly(System.Reflection.RuntimeAssembly, System.String[])
at System.AppDomain.nExecuteAssembly(System.Reflection.RuntimeAssembly, System.String[])
at System.Runtime.Hosting.ManifestRunner.Run(Boolean)
at System.Runtime.Hosting.ManifestRunner.NewThreadRunner()
at System.Threading.ThreadHelper.ThreadStart_Context(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.Threading.ThreadHelper.ThreadStart()
Followed by:
Faulting application name: applaunch.exe, version: 4.0.30319.33440, time stamp: 0x52003c5a
Faulting module name: KERNELBASE.dll, version: 6.3.9600.17415, time stamp: 0x54504ade
Exception code: 0xe0434352
Fault offset: 0x00014598
Faulting process ID: 0x1a64
Faulting application start time: 0x01d02c18ff5c4f43
Faulting application path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\applaunch.exe
Faulting module path: C:\WINDOWS\SYSTEM32\KERNELBASE.dll
Report ID: 3ee36614-980c-11e4-8354-b8ca3a87a278
Faulting package full name:
Faulting package-relative application ID:
What would be the most likely cause of this?
Thanks
The issue turned out to be caused by a problem with a user making a mistake with the versioning software (SVN). The result was several sections were removed from the .proj file resulting in the deployed package not being configured correctly.

Categories

Resources