0

I use a third party class with some static members:

class TheirClass{ //from metadata
    static TheirType TheirProp
}

I use instances of that class in my code

class MyWorkLoad  {
    TheirClass theInstance
}

But I require separate context / domains / whatsoever

{//My Program
    var w1 = MyWorkFactory.Create();
        w1.doWorkBasedOnFreshContext()

    var w2 = MyWorkFactory.Create();    
        w2.doWorkBasedOnFreshContext(); //err, dirty context, shared with w1
 }

I have tried creating child AppDomains but still getting the same behavior:

protected static AppDomain CreatedChildDomain(AppDomain parentDomain)
{
  Evidence evidence = new Evidence(parentDomain.Evidence);
  AppDomainSetup setup = parentDomain.SetupInformation;
  AppDomain.CreateDomain("Lab_"+generationCount, evidence, setup);
}

This is the factory method I use to instantiate

public static MyWorkLoad Create(string LabConfigPath)
{
    AppDomain subdomain = CreatedChildDomain(AppDomain.CurrentDomain);

    ExeConfigurationFileMap fileMap =
        new ExeConfigurationFileMap();

    fileMap.ExeConfigFilename = LabConfigPath;
    var config =
        ConfigurationManager.OpenMappedExeConfiguration(fileMap,
            ConfigurationUserLevel.None);

    var conf = config.AppSettings.Settings;
    var nextWork = new MyWorkLoad(subdomain, conf);
    generationCount++;
    return nextWork;
}

How may I achieve the desired isolation behavior?

F.I.V
  • 317
  • 1
  • 14
  • Please add the code of `MyWorkFactory`. – Chayim Friedman Dec 10 '18 at 09:10
  • AppDomains should provided the necessary isolation (assuming the library isn't doing something with unmanaged code) so how exactly are you using the appdomain that was created? – Mike Zboray Dec 10 '18 at 09:13
  • @ChayimFriedman: Thanks, code added. /@MikeZboray: As I am going to explain to you, I should guess that my usage shall be the problem, could you tell more what to do with the created domain? I must have missed something... – F.I.V Dec 10 '18 at 09:31
  • 1
    `TheirClass` must be a `MarshalByRefObject` in order the AppDomain magic to work. – dymanoid Dec 10 '18 at 09:33
  • @dymanoid: Thanks. In the case that I cannot change TheirClass (3rd party), does the magic work if I apply it on MyWorkLoad class instead? – F.I.V Dec 10 '18 at 09:49
  • 1
    It's unclear what you are doing with the `AppDomain` in your `MyWorkLoad` class. I see you're providing an `AppDomain` instance to that object's .ctor. But as such, each `nextWork` instance is created in the default AppDomain according to your code. Please provide your `MyWorkLoad` code. – dymanoid Dec 10 '18 at 10:25
  • @dymanoid: Thanks a lot. That mentioned uncleanness you emphasized, made it clear for me that I was missing something. So replacing the new instantiation with subdomain.CreateInstanceAndUnwrap solved the problem for me. Thanks a lot again for the magic. – F.I.V Dec 10 '18 at 14:38

0 Answers0