1

Some of our apps have a dependency on a common dll that is used for logging and configuration. This common dll is added to the GAC on our servers, and isn't included in the bin folder of our deployments.

I want to write a simple console app to check if common.dll has been added to the GAC on the servers. I have had no luck checking this programatically using fusion or assembly.load: Check GAC for an assembly

My idea was to include a reference to the dll locally, make sure copy local is off and then try catch the FileNotFoundException on the server when a call is made to a method in the dll.

Something like this

  static void Main()
        {
            try
            {                 
                Common.Logger.LogInfo("Testing logger."); //call to the dll
                Console.WriteLine("loaded successfully");
                Console.ReadLine();
            }
            catch (System.IO.FileNotFoundException e)
            {
                Console.WriteLine("Common dll missing!" + e.Message);
                Console.ReadLine();
            }
        }  

But as the dll isn't present the app will crash and throw a FileNotFoundException before even hitting the main method. I assume there is some initial checking that all dlls are present when running the .exe - is there a way to disable this?

Community
  • 1
  • 1
woggles
  • 7,444
  • 12
  • 70
  • 130

1 Answers1

2

Try moving it into a separate method:

static void Main()
{
    try
    {     
        CheckLogger();            
        Console.WriteLine("loaded successfully");
        Console.ReadLine();
    }
    catch (System.IO.FileNotFoundException e)
    {
        Console.WriteLine("Common dll missing!" + e.Message);
        Console.ReadLine();
    }
}  

static void CheckLogger()
{
    Common.Logger.LogInfo("Testing logger."); //call to the dll
}

You need the Main method to start running to enter the try/catch block and that won't happen if it cannot be (JIT) compiled. But as each method is compiled separately, this should work. The FileNotFoundException for missing assemblies is fired during compilation of methods that directly depend on the assembly.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448