将 DLL 的各个实例加载到应用程序:AppDomain、线程或其他东西?
我正在编写一个基于来自第三方(无源)的 COM 对象的应用程序,该对象包含在我的应用程序参考中。 COM 对象创建某些会计数据的镜像,以便可以以另一种格式(专有格式 -> MSSQL)读取它。应用程序是 COM 对象的包装器,允许在其中进行交互; COM 对象本身定期检查专有文件以查看它们是否已更新,如果已更新则复制它们。
我们有许多客户,每个客户都有自己的专有格式帐户。我们正在从以前的应用程序进行转换,一旦完成,我们将有大约 50 个客户端以这种方式运行。我已经有一个仅适用于一个客户端的应用程序,并且我不认为持续运行该应用程序的 50 个副本会有什么好处,特别是当 COM 似乎存在缓慢的内存泄漏时(啊)。因此,我尝试编写的程序将以一种或另一种方式分组的各种客户端,并在后台为每个客户端运行 COM,而前台允许进行额外的交互,并且如果需要,还可以重新启动 COM 对象。
起初我尝试使用 Threads/BackgroundWorker,这似乎工作正常,但经过一番欺骗后发现,尽管在为客户端创建的每个类上都将 COM 对象声明为 New,但所有客户端都共享相同的 COM 实例,因此只有最新加载的客户端才会执行任何操作。
虽然肯定可能是我做错了(如果可以的话,请这么说!如果有帮助,我会发布我的代码),但似乎我需要一种方法来加载单独的实例程序中每个客户端的 .DLL;可悲的是,这比我所处的水平更高。我一直在阅读应用程序域和反射,但从我所了解到的来看,我似乎无法轻松调用方法,并且可能无法使用事件(我依赖事件来更新信息) )
tl;dr: 加载 DLL 的单独实例以便我可以操作信息并侦听来自 COM 对象的事件的最佳方法是什么?
I am writing an application based on a COM object from a third party (no source), included in my application references. The COM object creates a mirror of certain accounting data so it can be read in another format (Proprietary format->MSSQL). The application is a wrapper for the COM object to allow for interaction within; the COM object itself routinely checks the proprietary files to see if they've been updated, then copies them if they have.
We have many clients, each with their own account for the proprietary format. We're in the middle of converting from a previous application, and once it's done we will have ~50 clients running this way. I already have an application that works for only one client, and I don't think it would be conducive to have 50 copies of the application running constantly, especially when the COM seems to have a slow memory leak (argh). As such, the program I'm trying to write will take various clients grouped in one manner or another and run the COM for each in the background, while foreground allows for additional interaction and, if need be, restarting the COM object.
At first I tried to use Threads/BackgroundWorker, and that seemed to work alright, but after some finagling it turns out that, despite the COM object being declared New on each class created for a client, all clients are sharing the same COM instance, so only the latest-loaded client will ever do anything.
While it could certainly be that I'm just doing it wrong (if I could be, please say so! I'll post my code if it would be helpful), it seems that I need to have a way to load a separate instance of the .DLL for each client in the program; sadly, this is a higher level than where I'm at. I've been reading into Application Domains and Reflection, but from what little I've been able to find out it doesn't seem that I can easily call methods and may not be able to use Events (which I rely on to update information in the application.)
tl;dr: What is the best way to load separate instances of a DLL such that I can manipulate the information and listen to events from the COM object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议加载到单独的
AppDomains
中。这将为您提供整个系统相当好的隔离性和可靠性。最重要的一点 - 故障处理,使用 AppDomains,您可以在出现未处理的错误时卸载任何特定的 AppDomain。否则,当多个组件加载到单独的线程/程序集中时,一个组件 (DLL) 中的问题可能会导致整个系统崩溃。有用的链接:
I would suggest loading into the separate
AppDomains
. This would give you a pretty good level of isolation and reliability over the entire system. Most important point - handling of the failures, using AppDomains you would be able unloading any particularAppDomain
in case of unhandled error. Otherwise issue in one component (DLL) potentially could crash entire system with multiple components loaded into separate threads/assemblies.Useful Links: