0

I have an application, designed to run on a 24/7 server. Localy tested everything just works smooth and as expected. Deployed to the Windows Server 2012 R2 - I get an exception, whenever the main task (threaded) starts.

On the Server i have obviously no Visual-Studio, so I cannot debug ig. I Just get an Ugly Windows-Dialog, stating that "{ApplicationName} has stopped working.".

In the EventViewer i found an Entry, indicating a position of the error.

The EventLog says:

Application: MyApplicationBackend.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
Stack:
   at Application.Name.SomeClass..ctor()
   at Application.Name.SomeThread()
   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()

Well, inside the constructor of SomeClass there is nothing critical going on - just a mysql database connection, like this, (Where cfg. is a singleton, holding Configuration values):

 //Connecto to server.
 String connectionString = "server=" + cfg.DatabaseServer + ";user=" + cfg.DatabaseUser + ";database=" + cfg.DatabaseName + ";port=" + cfg.DatabasePort + ";password=" + cfg.DatabasePassword + ";";
 mysqlConnection = new MySqlConnection(connectionString);
  • MySQL Connector .Net 6.8.3 is installed
  • Code is going to connect to a remote database (local network)
  • Code works perfect on my local machine. (Same network)

This raises 2 Questions:

1.) Can I (and if, how?) handle Exceptions that appear within any level of nested threads from the main-thread?

2.) What kind of file could it be, that is not found (regarding this 2 lines of code)? Localy its working fine (soure is located on a network share), but when executing from my server i get this exception (Using the SAME exectuable from the SAME network share)


Just used some MessageBoxes to debug the stuff... Ultimately it looks like the following:

public class class1{
   private void SomeThread(){
       MessageBox.Show("before con");
       DbCon dc = new DbCon();
       MessageBox.Show("after con");
   }
}

and

  public class DbCon{
      public DbCon(){
             MessageBox.Show("inside");
             String connectionString = "server=" + cfg.DatabaseServer + ";user=" + cfg.DatabaseUser + ";database=" + cfg.DatabaseName + ";port=" + cfg.DatabasePort + ";password=" + cfg.DatabasePassword + ";";
             mysqlConnection = new MySqlConnection(connectionString);
      }
   }

I receive the Message "before con" - And then the exception is thrown... Like it just does not find DbCon-Class - but this is part of the executable - and could not trigger a File-Not-Found-Exception??!! (AND works on local machine :-( ) ...

dognose
  • 20,360
  • 9
  • 61
  • 107
  • Maybe `cfg` tries to read its configuration from a file, and that file is missing on the server? – Medo42 Jul 10 '14 at 22:17
  • @Medo42, that is onfirmed to work. (Yes, singleton is reading from a file, but I read that on app startup and only write back on app close) – dognose Jul 10 '14 at 22:33
  • Next try, maybe it can't find the `MySqlConnection` class? Since execution doesn't appear to reach the actual constructor code but everything up to the call to the constructor, it might fail to load a class referenced there. Then again, if `mysqlconnection` is of the type `MySqlConnection` then it should probably already fail in the cctor... – Medo42 Jul 10 '14 at 22:49
  • @Medo42 yes, `mysqlConnection` is a private member. That would explain why it fails actually before the Ctor, but after the ctor-call. Will check mysql connector installation and references on that system. – dognose Jul 11 '14 at 08:26
  • @Shiva it is initializing it's members - `mysqlConnection` is just a private member. Connection is just initialized, not connected. – dognose Jul 11 '14 at 08:27

1 Answers1

1

1) yes, you can - though usage of it is highly advised against, you can catch any exception at any time by hooking into the FirstChanceException Event.

AppDomain.Current.FirstChanceException += (sender, e) => { ... };

Note this will also fire for exceptions that are then handled, including ones handled by .NET under the hood, and could slow you application down a bit. It's usually not recommended, but it'll catch absolutely everything being thrown in the managed code of your application, both inside and outside your assemblies, making it absolutely brilliant to debug with.

EDIT: If you want this to attach as easrly as possible, put your Main into a static class and the event hook into a static constructor. This forces it to happen so early, it can pre-empt DLL loads.

2) There are a number of reason it can't find a file, assuming you load your configuration from a file.

  • If you're executing on a server, make sure the program is running as admin or with the rights needed to access the filesystem. This is a common problem on a network share
  • Make sure you're not trying to reference the file with a local path, such as "C:\ProgramData" or "%ProgramData%" as these will look into the C drive of the executing machine.
  • If you're using any DLLs from the GAC, copy them into the output folder instead - this can cause an exception (though usually this is a DllNotFoundException) if the server doesn't have the files loaded or has a different version loaded.

If you have access, also try remoting into the server and debugging locally, as this can help reveal problems with the actual deployment environment. There are also ways to debug on a remote machine that may prove useful for your situation.

David
  • 10,458
  • 1
  • 28
  • 40
  • (I know this answer is old, but) ... *FirstChanceException Event: "though usage of it is highly advised against"* -- by whom, why? (Because I'm currently looking into [this](http://stackoverflow.com/q/35503673/321013).) – Martin Ba Feb 19 '16 at 12:22
  • @MartinBa Usage is highly advised against by myself in this case (and most others I've seen mention it on SO). This is because that while it may be useful for debugging, it also has the "catches everything" problems mentioned above, and the documentation lists some serious corruptions if the handler itself hits an exception. The chances of hitting handled exceptions are also so high, that when I activate it in production code, I usually only activate the breakpoint right before reproducing a bug, rather than at program startup. – David Feb 19 '16 at 14:31