如何防止.NET中DLL的智能加载?
我正在开发的程序有一些问题。它由 2 个 DLL 组成,其中 dll A 引用 dll B。DLL A 包含一个公共方法,其中第一个操作(在实例化 B 中的任何类之前)是检查某个网络位置以查看是否有新版本的 dll B 可用。如果是这样,它将在当前 B 的同一位置下载它,这应该不是问题,因为 B 中的任何内容都没有被实例化。遗憾的是,它是实例化的,因此我收到一个错误,它已被拥有 A 的进程引用并且无法替换。
您是否知道它已被引用的原因,以及是否有任何解决方案可以避免这种情况?
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;
}
I have some issue with a program I'm working on. It's composed of 2 DLLs, with dll A referencing dll B. Dll A contains one public method, in which first action (before instanciating any class in B) is to check some network location to see if a new version of dll B is available. If so, it downloads it at the same location of current B, which should not be a problem since nothing from B is instanciated. Sadly, it is instanciated and so I get an error it is already referenced by the process that owns A and cannot be replaced.
Do you have any idea of the reason why it is already referenced, 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Jit 编译器需要加载 Dll B,以便检查/验证 Supervise 方法。
将对 Dll B 的调用移至另一个方法,并阻止此方法避免被内联 (
[MethodImpl (MethodImplOptions.NoInlined)]
)。否则,从调试模式切换到发布模式时可能会出现奇怪的效果。如果我没记错的话,内联不会用于 Debug 编译的代码,但发布代码可能会内联被调用的方法,从而使抖动在检查之前加载 Dll B。
The Jit compiler needs to load Dll B, in order to check/validate the
Supervise
method.Move calls to Dll B into another method, and prevent this method from being inlined (
[MethodImpl(MethodImplOptions.NoInlining)]
). Otherwise you might have strange effects switching from Debug to Release mode.If I remember it correctly, inlining is not used for Debug compiled code, but release code might inline the called method, making the jitter load Dll B before the check.
当你的“Supervise”方法被 JITted 时,B dll 将被加载。这里的问题是,DLL 是在第一次需要 B.dll 中某些类型的类型信息时加载的,而不是在第一次实例化对象时加载。
因此,在引用 B.dll 中的任何类型之前以及调用使用 B.dll 中的类型的任何方法之前,必须检查更新。
By the time your "Supervise" method is JITted, the B dll will be loaded. THe issue here is that the DLL is loaded the first time type information for some type in B.dll is needed, not the first time an object is instantiated.
So you must check for update before you reference any type in B.dll, and before you call any methods that use a type in B.dll.