Need 2 copies of a static class from an external provider dll

We use an external provider dll to contact them as part of our application. This DLL has a static class that includes some properties that define the account (credentials) that is used to connect to the provider. However, our application must connect using two different accounts, depending on the code path. That would be nice (we would just set up the correct account right before each connection), but the problem is that we also need to have event handlers associated with the provider events (which are also in this static class), and the event handlers should respond events triggered for each account. The presence of one copy of the static class means that only events from the currently connected account are accepted.

I know that this is not a very good design (if it depended on me, the provider class would be created twice, once for each account), but we do not have control over how the supplier developed its dll, and they are not going to change it.

It seems the only way to have event handlers for events for both accounts is to have two copies of our application, but it is really ugly. Is there a way to somehow have two dll copies referenced by our project? Or any other way to solve this problem?

+4
source share
3 answers

, , AppDomains. AppDomain. , , . . :

.NET?

AppDomains (object.Event + = ;)

+3

. , , dll , . , .

, . .

AppDomain ?

, , AppDomain

+2

AppDomain. , Foo.:

public static class Foo {
  public static event EventHandler<EventArgs> Bar;
  public static string Message;

  public static void RunBar() {
    var bar = Bar;
    if (bar != null) {
      bar.Invoke(null, EventArgs.Empty);
    }
  }
}

, , :

public interface IFooWrapper {
  event EventHandler<EventArgs> Bar;
  string Message { get; set; }
  void RunBar();
}

, / Foo.

public class FooWrapper : MarshalByRefObject, IFooWrapper {
  public string Message {
    get { return Foo.Message; }
    set { Foo.Message = value; }
  }
  public event EventHandler<EventArgs> Bar;

  public FooWrapper() {
    Foo.Bar += (sender, args) => {
      var myBar = Bar;
      if (myBar != null) {
        myBar(sender, args);
      }
    };
  }

  public void RunBar() {
    Foo.RunBar();
  }
}

, - FooWrapper , IFooWrapper

  var domain1 = AppDomain.CreateDomain("Foo1");
  var domain2 = AppDomain.CreateDomain("Foo2");
  var foo1 = (IFooWrapper)domain1.CreateInstanceAndUnwrap("ConsoleApplication2", "ConsoleApplication2.FooWrapper");
  foo1.Message = "Foo1";
  foo1.Bar += (sender, eargs) => {
    Console.Out.WriteLine("foo1 bar called");
  };
  var foo2 = (IFooWrapper)domain2.CreateInstanceAndUnwrap("ConsoleApplication2", "ConsoleApplication2.FooWrapper");
  foo2.Message = "Foo2";
  foo2.Bar += (sender, eargs) => {
    Console.Out.WriteLine("foo2 bar called");
  };

:

// writes Foo1
Console.Out.WriteLine(foo1.Message);
// writes Foo2
Console.Out.WriteLine(foo2.Message);
// writes foo1 bar called
foo1.RunBar();
// writes foo2 bar called
foo2.RunBar();
// does nothing
Foo.RunBar();
+2
source

All Articles