How to resolve NuGet dependency hell

后端 未结 2 2077
南笙
南笙 2020-12-22 22:40

I develop a library with some functional named CompanyName.SDK which must be integrated in company project CompanyName.SomeSolution

C

2条回答
  •  萌比男神i
    2020-12-22 23:09

    You can work at post-compilation assembly level to solve this issue with...

    Option 1

    You could try merging the assemblies with ILMerge

    ilmerge /target:winexe /out:SelfContainedProgram.exe Program.exe ClassLibrary1.dll ClassLibrary2.dll

    The result will be an assembly that is the sum of your project and its required dependencies. This comes with some drawbacks, like sacrificing mono support and losing assembly identities (name, version, culture etc.), so this is best when all the assemblies to merge are built by you.

    So here comes...

    Option 2

    You can instead embed the dependencies as resources within your projects as described in this article. Here is the relevant part:

    At run-time, the CLR won’t be able to find the dependent DLL assemblies, which is a problem. To fix this, when your application initializes, register a callback method with the AppDomain’s ResolveAssembly event. The code should look something like this:

    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
    
       String resourceName = "AssemblyLoadingAndReflection." +
    
          new AssemblyName(args.Name).Name + ".dll";
    
       using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
    
          Byte[] assemblyData = new Byte[stream.Length];
    
          stream.Read(assemblyData, 0, assemblyData.Length);
    
          return Assembly.Load(assemblyData);
    
       }
    
    };
    

    Now, the first time a thread calls a method that references a type in a dependent DLL file, the AssemblyResolve event will be raised and the callback code shown above will find the embedded DLL resource desired and load it by calling an overload of Assembly’s Load method that takes a Byte[] as an argument.

    I think this is the option i would use if I were in your shoes, sacrificing some initial startup time.

    Update

    Have a look here. You could also try using those tags in each project's app.config to define a custom sub-folder to look in when the CLR searches for assemblies.

提交回复
热议问题