问题描述
我正在一个项目中,我必须在运行时加载程序集(让它们称为任务),运行该任务,然后能够终止整个程序集,以便可以在不中断主应用程序的情况下替换dll. >
在主应用程序中有许多任务正在运行,有些任务是顺序运行的,而有些则是并行运行的.有时,其中一项或多项任务需要更新,然后重新添加到队列中.当前,我们正在停止整个应用程序,并在处于其他阶段的其他任务之间进行交互,这并不理想.
我已经知道必须将每个程序集加载到单独的AppDomain中,但是事实证明在这些域中加载程序集非常困难,尤其是当我需要实际运行任务并从中接收事件时.我已经研究了这个问题几天,但仍然没有获得有效的概念证明.
我有一个任务接口,其中包括一个运行"和杀死"方法(subs)以及一个任务步骤",完成"和被杀死"事件. ``taskstep''返回一个对象以供稍后缓存,完成整个任务后将触发``complete'',而准备卸载时将触发``killed''.万一它被卡住,整个过程还应该有两个小时的超时,而被终止的事件应该有一个2分钟的超时,这时我希望能够卸载它,迫使任何线程终止(这是什么)无论如何,杀"应该做.每个程序集可能包含多个任务,所有任务都应可装载.
将这些任务作为插件"加载时,我没有问题,但是在尝试同时使用和卸载它们时迷失了方向.如果我必须创建一些精致的包装器,那么就可以了,但是我什至需要吗?
(PS.我希望我可以在c#中做到这一点,但有人告诉我VB是更可取的.如果只有在c#中可行,那么对我来说就更好了,但对我的老板来说不是那么好^ _ ^)
非常感谢
Andy
Hi,
I am working on a project where I have to load assemblies (lets call them tasks) at runtime, run the task and then be able to kill off the whole assembly so that the dll can be replaced without interupting the main application.
There are many of these tasks running within the main application, some run sequentially and some run in parallel. Occasionally one or to of these tasks need to be updated and then re-added to the queue. Currently we are stopping the entire application and interupting the other tasks at whatever stage they are at, which is not ideal.
I have figured out that I will have to load each assembly into a seperate AppDomain, but loading the assemblies in those domains is proving difficult, especially when I need to actually run the tasks and receive events from them. I have been looking into this problem for a couple of days and have still not managed to get a working proof-of-concept.
I have an Interface for the tasks which includes a ''run'' and ''kill'' method (subs) and a ''taskstep'', ''complete'' and ''killed'' event. ''taskstep'' returns an object to be cached later, ''complete'' fires when the whole task is done and ''killed'' fires when it is ready to be unloaded. There should also be a timeout on the whole process of two hours and a timeout of 2 minutes on the killed event incase it gets stuck, at which point I would like to be able to unload it, forcing any threads to terminate (which is what ''kill'' should do anyway). Each assembly may contain several tasks, all of which should loadable be unloadable.
I have no problems loading these tasks as ''plugins'' but am lost when trying to both use them and unload them. If I have to create some elaborate wrapper then so be it but is what I need even possible?
(PS. I wish I could do this in c# but I have been told that VB is preferable. If it is only possible in c# then all the better for me, but not so much for my boss ^_^)
Many thanks
Andy
推荐答案
string pathToAssembly = Path.Combine(Application.StartupPath, "myAssemby.dll");
object[] parameters = null;
Assembly assembly = Assembly.LoadFile(pathToAssembly);
Type type = assembly.GetType("MyAssembly.Form1");
MethodInfo methodInfo = type.GetMethod("ShowDialog", Type.EmptyTypes);
object instance = Activator.CreateInstance(type);
methodInfo.Invoke(instance, parameters);
瓦莱里.
Valery.
这篇关于在运行时加载,使用和卸载装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!