关于.net影子复制的问题
发布日期:2021-08-17 10:07:56
浏览次数:43
分类:技术文章
本文共 2677 字,大约阅读时间需要 8 分钟。
I have the following problem when creating an and loading an assembly: I followed the steps described as anywhere i looked for documentation regarding this issue and the result is not quite what I expected. I obtain a shadow copy of my dll, but both files, the original and the shadow copied dll are locked by the process. Here is a piece of the code, please tell me what is wrong with it and what should i do for the original dll not to be locked anymore System. appDomainSetup = new (); appDomainSetup. Shadow CopyDirectories = "D:\\pathToMyApp\\bin\\Debug"; appDomainSetup. Shadow CopyFiles = "true"; appDomainSetup.CachePath = "D:\\pathToMyApp\\bin\\Debug"; appDomainSetup.ApplicationName = "appName"; System. appDomain = System. .CreateDomain("Domain" + "someName", new System.Security.Policy. ( .CurrentDomain.Evidence), appDomainSetup); System.Reflection. assemblyName = System.Reflection. .GetAssemblyName("pathToTheDll"); System.Reflection. assembly = appDomain.Load(assemblyName); I also tried using the Load(byte[] buffer) version of .Load, but in this case, the shadow copy is not created anymore but the original dlls is used by the application, which, by the way, seems very strange to me. --------------------------------------------------------------------------------- The problem lies in the way .Load() works. When you make a call to .Load, the assembly is loaded in the target domain (which is appDomain in your case) BUT the call returns a reference to the same assembly back to your parent domain (the one where the code is running). When this happens, the dll gets loaded in the parent domain where there is no setting to shadow copy the files. So you see, shadow copy is working for the target domain, but it doesn't happen in the parent domain. Hence, the file gets locked. If you want to load the assembly in the target domain without getting a reference back in the parent domain, you will have to write a class that will load the assembly for you in the target domain. i.e., something like a proxy object where you would first create an instance of the Loader object in the target domain and then call Loader.LoadMyAssembly(filename) which would load the specified file in the target domain and NOT return any reference to it. The only caveat is that you should never get a direct reference to the loaded assembly, even if it just an interface. The moment you do that, it will get loaded in your parent domain and you're back at square one. There is an excellent article on this topic by Eric Gunnerson on MSDN called ' AppDomains and Dynamic Loading'. You should take a look at that.
转载于:https://www.cnblogs.com/jmax/archive/2009/09/28/1575765.html
转载地址:https://blog.csdn.net/weixin_30846599/article/details/99222560 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月04日 20时19分14秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ACCEPT()和ACCEPT4()
2019-04-27
php内核探索方法与资源
2019-04-27
PHP安装扩展mcrypt以及相关依赖项 【PHP安装PECL扩展的方法】
2021-06-30
Javascript到PHP加密通讯的简单实现
2021-06-30
德国SNS交友/视频网站Poppen.de的技术架构分享
2021-06-30
UNIX环境编程
2021-06-30
一笔画问题【数据结构-图论】
2021-06-30
红黑树
2021-06-30
安装多个gcc
2021-06-30
Linux0.01内核根目录Makefile注释
2021-06-30
【CSDN2012年度博客之星】需要您的一票,感谢大家的支持
2021-06-30
PHP对于浮点型的数据需要用不同的方法去解决
2021-06-30
Tokyo Cabinet 安装
2021-06-30
Flink在美团的应用与实践听课笔记
2019-04-27
Java多线程的11种创建方式以及纠正网上流传很久的一个谬误
2019-04-27
JDK源码研究Jstack,JMap,threaddump,dumpheap的原理
2019-04-27
Java使用字节码和汇编语言同步分析volatile,synchronized的底层实现
2019-04-27
为什么选择RocketMQ以及Confluent公司对Kafka的分区设计的说明
2019-04-27
PHP中魔术方法的用法
2019-04-27
写个CSDN站内文章搜索功能
2019-04-27