Search code examples
.netconfigurationattributesassemblies

Start exe even with missing dependency dlls?


In Dotnet2.0 and later a program refuses to start if one of its dependent (static referenced) dlls are missing.

With Dotnet1.1 and 1.0 the program started but crashed later when trying to use functionality of the missing assembly.

I wonder if there is something like a

  • compiler switch ,
  • configuration option or
  • a dotnet [attribute]

to allow me to start the app when certain dlls are missing.

Is it possible without moidfying the sourcecode (execpt by applying some Attriutes)?

I don't want to manualy load assemblies by programcode or use IOC-Framworks.

Update: With "static referenced dlls" i mean the opposite of dynamicly loading a dll in my own programcode using reflection and Assembly.Loadxxxx().

Update 2010-12-25 I was thinking to complicated. Thanks simple solution from @erinus:

i just have to put try catch around and it worked:

    using System;
    using System.IO;
    using log4net; // log4net.dll might be missing

    namespace ConsoleAppWithMissingDll
    {
        class Program
        {
            static bool dllIsInstalled = true;
            static void Main(string[] args)
            {
                Console.WriteLine("Hello missing dll");

                try
                {
                    OutputViaLog4Net("hello log4net");
                }
                catch (FileNotFoundException)
                {
                    dllIsInstalled = false;
                    Console.WriteLine("Log4net-dll not found");
                }
                Console.WriteLine("Program continued");
    #if DEBUG
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
    #endif
            }

            private static void OutputViaLog4Net(string message)
            {

                ILog logger = LogManager.GetLogger("MyLogger");

                logger.Debug(message);

            }
        }
    }

Solution

  • Use unmanaged code. Call Windows API: LoadLibrary in try{...}catch{...} block. If dll is missing, handle exception and keep the process run.