I have a C# .NET application that I'm maintaining and adding new features to. One of the new features requires the use of a third party DLL which may not always be present when the application starts. I've added the DLL as a reference to the project along with code to detect if the DLL is missing. That code notify the user about the missing DLL file BEFORE any code referencing the actual DLL is called. Unfortunately, something in the native runtime attempts to access the DLL before main() even runs and throws an exception. Here's some output from the debugger:
Your app has entered a break state, but no code is currently executing that is supported by the selected debug engine (e.g. only native runtime code is executing).
System.IO.FileNotFoundException: 'Could not load file or assembly 'FTD2XX_NET, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.'
The environment is supposed to only access the assemblies on demand. Since something in the CLR is throwing an exception before main() even runs the application has no chance to even check for the existence of the DLL. Since none of the application code has run yet why would the DLL be referenced?
Can anyone provide some insight on why the native runtime is throwing this exception and how it can be avoided? To my knowledge there is no longer functionality for delaying DLL loading. Is that the case or is there a secret way of still doing that? I'd settle for a way catch this exception so the user could be notified. Thanks in advance for any information.
Information on the system in question:
- Window 10, x64
- .NET Framework 4.6.1
- Visual Studio 2019
- I can't update to a newer version of .NET or Visual Studio.