We are using an external vendor's dll to communicate with them as part of our application. This dll has a static class, which includes some properties that determine the account (credentials) that is used to connect to the vendor. However, our application needs to connect using two different accounts depending on the code path. This would be fine (we would just set the correct account right before each connection), but the problem is that we also need to have event handlers tied to the vendor's events (which are also on this static class) and the event handlers need to respond to events fired for each account. Having one copy of the static class means that only events from the currently connected account get picked up.
I know this is not a good design (if it were up to me, the vendor's class would be instantiated twice, once for each account), but we don't have control over how the vendor designed their dll and they are not going to change it.
It seems like the only way to have event handlers watch for events for both accounts is to have two copies of our application, but that's really ugly. Is there any way to somehow have two copies of the dll referenced from our project? Or any other way to solve this issue?
You may be able to achieve what you need with AppDomains. Static instances are not shared across AppDomain boundaries. And you can write code that will subscribe to events from different appdomains but there are some complications with remoting. See those questions and answers for more info:
What is a .NET application domain?
How can I subscribe to an event across AppDomains (object.Event += handler;)
From reading it. I Feel like it could be done by loading the dll into two separate app domains which would allow you to instantiate the class separately in each domain. With that said I have never done it.
This questions has code that should be able to lead to answers I will try to create a sample and edit tonight.
How to use an AppDomain to limit a static class' scope for thread-safe use?
I was writing the example for it but found one that works out of the box Static Fields in AppDomain
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With