Kodithic
Kodithic

Reputation: 170

Load individual instances of DLL to application: AppDomain, Threads, or something else?

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?

Upvotes: 1

Views: 288

Answers (1)

sll
sll

Reputation: 62524

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 particular AppDomain 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:

Upvotes: 1

Related Questions