2

I have the following class (shortened for easiness of reading)

public class Connection
{
    Guid _id;
    AppDomain _appDomain;
    Type _coreApp;

    public string ConnectionName
    {
        get
        {
            this._coreApp.InvokeMember("_some_property_", BindingFlags.GetProperty, null, null, null).ToString();
        }
    }

    public Connection(string username, string password)
    {
        this._id = Guid.NewGuid();
        this._appDomain = AppDomain.CreateDomain(this._id.ToString());
        Assembly asm = this._appDomain.Load("_some_dll_");
        this._coreApp = asm.GetExportedTypes().First(t => t.Name == "_some_type_");
        this._coreApp.InvokeMember("_some_method_", BindingFlags.InvokeMethod, null, null, new object[] { username, password });
    } 
}

I also have the following code

public class Main()
{
    public Main()
    {
        Connection connOne = new Connection("_some_user_1_", "_some_pw_1_");
        Connection connTwo = new Connection("_some_user_2_", "_some_pw_2_");

        string nameOne = connOne.ConnectionName;
        string nameTwo = connTwo.ConnectionName;
    }
}

I also got these facts:

  • The dll I'm loading into the AppDomain is a 3rd party
  • _some_method_ and _some_property_ are static
  • ConnectionName should return different values for both instances as the parameters aren't the same.

And last I got this issue:

I was working under the assumption that calling a static method from a Type within a DLL in its own AppDomain, would isolate that call from others to the same static method in the same DLL in a separate AppDomain, but for some reason this isn't the case. If I run the code like that I would, for example, get both strings as "result_1", inverting the parameters would set both strings to "result_2".

I basically need to totally isolate the dlls on each instance of Connection as there are a lot of static things going on and I can't have one changing the other, as is the case.

I'm not sure that code even compiles, please ignore any grammar or semantic issue as I can't post the actual code and had to write this on the fly and on the browser.

PedroC88
  • 3,708
  • 7
  • 43
  • 77
  • Can you show where you load the type into a separate AppDomain? – Ron Beyer Apr 30 '15 at 03:02
  • Isn't the line _this._appDomain = AppDomain.CreateDomain(this._id.ToString());_ creating one AppDomain for that instance of _Connection_? And then isn't _this._coreApp = asm.GetExportedTypes().First(t => t.Name == "_some_type_");_ instantiating a type from THAT AppDomain? – PedroC88 Apr 30 '15 at 03:04
  • Sorry, missed those lines when scanning, late night. – Ron Beyer Apr 30 '15 at 03:59
  • 1
    see this: http://stackoverflow.com/questions/4298913/static-fields-in-appdomain – Ron Beyer Apr 30 '15 at 04:01
  • @RonBeyer feel free to add that comment as an answer as I found there what I was looking for, thanks. – PedroC88 Apr 30 '15 at 18:51
  • I would if I could, but the question is closed as a duplicate. Glad you got it. – Ron Beyer Apr 30 '15 at 18:54

1 Answers1

2

I agree with @RonBeyer. All of your code and your method/property invocations are running in the same AppDomain as that of your Main class. Just the fact that you are instantiating a new AppDomain and doing an AppDomain.Load does not mean that you are now running code inside of that other AppDomain that you created. Ultimately all you are doing is getting the exported types from the assembly, and then invoking a property/method on one of those types. But nowhere are you expressing that the invocation is to occur in the other AppDomain.

See these posts for some info on making cross-AppDomain calls:

Simplest way to make cross-appdomain call?

Cross AppDomain call is executed in caller Domain

Community
  • 1
  • 1
Rajeev Goel
  • 1,395
  • 8
  • 8
  • Thanks @Rajeev, even though they didn't answer my specific question they were very helpful in understanding my issue. – PedroC88 Apr 30 '15 at 18:52