AppDomain 卷影复制 - 加载/卸载动态加载的 Dll
我试图动态加载 dll 的下面的代码不起作用。
AppDomain appDomain = AppDomain.CreateDomain("DllDomain");
Assembly a = appDomain.Load(fileName);
//Assembly a = Assembly.LoadFrom(fileName);
objType = a.GetType(className);
obj = a.CreateInstance(className);
object[] args = new object[1];
args[0]=(object) "test";
object ret = objType.InvokeMember("Perform", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, args);
string output = ret.ToString();
obj = null;
AppDomain.Unload(appDomain);
这是我在 WCF 服务中使用的代码,但它仍然不起作用。
听说我们可以在 AppDomain 中使用“影子复制”来实现。但我对“影子复制”以及如何在上面的代码中实现相同的内容一无所知。
请提供工作代码作为“影子复制”的示例。
-BS
The Code as below which i'm trying to load a dll dynamically is not working.
AppDomain appDomain = AppDomain.CreateDomain("DllDomain");
Assembly a = appDomain.Load(fileName);
//Assembly a = Assembly.LoadFrom(fileName);
objType = a.GetType(className);
obj = a.CreateInstance(className);
object[] args = new object[1];
args[0]=(object) "test";
object ret = objType.InvokeMember("Perform", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, args);
string output = ret.ToString();
obj = null;
AppDomain.Unload(appDomain);
this is the code i am using inside a WCF service but still it does not work.
Heard that we can acheive using 'Shadow Copying' in AppDomain. But i dont know anything about 'Shadow Copying' and how to implement the same in the above code.
Please provide working code as example for 'Shadow Copying'.
-B.S.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将程序集加载到应用程序域中,但无法从该域中卸载它们。
但是,在一个应用程序域中,您可以创建第二个应用程序域并将程序集加载到第二个应用程序域中。稍后您可以选择卸载第二个应用程序域,这又会卸载您加载到第二个应用程序域中的程序集。
这是基本原则。在实践中,您会发现许多障碍(它们随着 .NET 版本的不同而发生变化)需要解决,特别是当您在应用程序域之间建立某种形式的通信时。
在这里提供工作代码可能会太大。
You can load assemblies into an application domain but you cannot unload them from that domain.
However, in one application domain you can create a second application domain and load an assembly into the second application domain. Later you can then choose to unload the second application domain which in turn unloads the assembly that you loaded into the second application domain.
This is the basic principle. In practice you will find a number of obstacles (they changed through the versions of .NET) to be resolved in particular when you set up some form of communication between the application domains.
Providing working code here would probably be too big in size.