Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

            }
        }
    }
like image 849
k3b Avatar asked Dec 18 '10 08:12

k3b


2 Answers

A "statically referenced dll" is an oxymoron, the d in dll means "dynamic". There are implicitly referenced dlls but only unmanaged code uses those. You cannot start a program with such a DLL missing, the dll is loaded before the program's entrypoint starts executing.

.NET loads dlls on demand, triggered by the JIT compiler. As soon as it compiles a method of a type that's stored in that DLL will the DLL be loaded. Shipping code with such a DLL missing is technically possible, you have to be careful to write your code so that a type from such a DLL is never used. This is the desktop version behavior, not quite sure if the CF version works the same way.

like image 120
Hans Passant Avatar answered Sep 18 '22 13:09

Hans Passant


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

like image 39
erinus Avatar answered Sep 18 '22 13:09

erinus