How to prevent intelligent DLL loading in .NET?

I have a problem with the program I'm working on. It consists of 2 DLLs with dll A referencing dll B. Dll A contains one public method in which the first step (before running any class in B) is to check some network location to see if a new version of dll B is available If so, it loads it in the same location of current B, which should not be a problem, since nothing of B is backed up. Unfortunately, this is due, and therefore I get an error message that is already referenced by a process that owns A and cannot be replaced.

Do you have any idea of ​​the reason it has already been mentioned, and if there is any solution to avoid this?

public class L10nReports//Class in DLL A { public L10nReports() //constructor { } //only public method is this class public string Supervise(object projectGroup, out string msg) { //Checks for updates of dll B and downloads it if available. And fails. manageUpdate(); //first instanciation of any class from dll B ReportEngine.ReportEngine engine = new ReportEngine.ReportEngine(); string result = engine.Supervise(projectGroup, out msg); return result; } 
+4
source share
2 answers

The Jit compiler must load Dll B in order to check / verify the Supervise method.

Move calls in Dll B to another method and prevent this method from being embedded ( [MethodImpl(MethodImplOptions.NoInlining)] ). Otherwise, you may have strange effects switching from Debug to Release mode.

If I remember it correctly, inlining is not used for compiled Debug code, but the release code can embed the called method, putting a strain on the Dll B jitter before checking.

  //only public method is this class // all calls to dll B must go in helper function public string Supervise(object projectGroup, out string msg) { //Checks for updates of dll B and downloads it if available. And fails. manageUpdate(); return SuperviseHelper(projectGroup, out msg); } [MethodImpl(MethodImplOptions.NoInlining)] public string SuperviseHelper(object projectGroup, out string msg) { //first instanciation of any class from dll B ReportEngine.ReportEngine engine = new ReportEngine.ReportEngine(); string result = engine.Supervise(projectGroup, out msg); return result; } 
+5
source

By the time your Observation method is JITted, dll d will be loaded. The problem is that the DLL loads the first time that type type information in B.dll is needed, and not the first time an object is instantiated.

So, you should check for updates before referring to any type in B.dll and before calling any methods that use the type in B.dll.

 public class L10nReports//Class in DLL A { public L10nReports() //constructor { } //only public method is this class public string Supervise(object projectGroup, out string msg) { manageUpdate(); return SuperviseImpl(projectGroup, out msg); } private string SuperviseImpl(object projectGroup, out string msg) { //first instanciation of any class from dll B ReportEngine.ReportEngine engine = new ReportEngine.ReportEngine(); string result = engine.Supervise(projectGroup, out msg); return result; } 
+6
source

Source: https://habr.com/ru/post/1345115/


All Articles