views:

166

answers:

1

I would like to share my AppSettings from Application.exe.config to be shared/used by the newly created AppDomains . I am creating AppDomains as shown below

  public static AppDomain Create(Guid sessionId)
  {
     AppDomain currentDomain = AppDomain.CurrentDomain;

     AppDomainSetup setup = new AppDomainSetup();

     // use the ID as part of the unique name for the process
     string name = "Session_" + sessionId;

     setup.ApplicationName = name;
     setup.ApplicationBase = currentDomain.SetupInformation.ApplicationBase;
     setup.PrivateBinPath = currentDomain.SetupInformation.PrivateBinPath;
     setup.ConfigurationFile = currentDomain.SetupInformation.ConfigurationFile;

     Evidence baseEvidence = currentDomain.Evidence;
     Evidence evidence = new Evidence(baseEvidence);

     return AppDomain.CreateDomain(name, evidence, setup);
  }

Do I need to have any additional steps to have ConfigurationManager.AppSettings[key] return the same values as the original AppDomain ?

A: 

No you don't need to do anything special.

Using the following code to isntantiate a server object:

var instance = (ServerClass)domain.CreateInstance("ClassLibrary1", "ServerClass").Unwrap();

var result = instance.ReadConfig();

from the following class in a seperate assembly:

[Serializable]
public class ServerClass : MarshalByRefObject
{
    public ServerClass() { }


    public string ReadConfig()
    {
        var foo = ConfigurationManager.AppSettings["foo"];
        Console.WriteLine(String.IsNullOrEmpty(foo) ? "null" : foo);

        return foo;
    }
}

returns the value for the appsetting in the app.config from the executable (i.e. the main appdomain).

pb