App config for dynamically loaded assemblies

前端 未结 3 1081
执念已碎
执念已碎 2021-02-04 09:42

I\'m trying to load modules into my application dynamically, but I want to specify separate app.config files for each one.

Say I have following app.config setting for ma

3条回答
  •  遥遥无期
    2021-02-04 10:26

    As far as I know, you need separate application domains for the app.config to work separately. The creation of an AppDomainSetup allows you to specify which config file to use. Here's how I do it:

    try
    {
      //Create the new application domain
      AppDomainSetup ads = new AppDomainSetup();
      ads.ApplicationBase = Path.GetDirectoryName(config.ExePath) + @"\";
      ads.ConfigurationFile = 
        Path.GetDirectoryName(config.ExePath) + @"\" + config.ExeName + ".config";
      ads.ShadowCopyFiles = "false";
      ads.ApplicationName = config.ExeName;
    
      AppDomain newDomain = AppDomain.CreateDomain(config.ExeName + " Domain", 
        AppDomain.CurrentDomain.Evidence, ads);
    
      //Execute the application in the new appdomain
      retValue = newDomain.ExecuteAssembly(config.ExePath, 
        AppDomain.CurrentDomain.Evidence, null);
    
      //Unload the application domain
      AppDomain.Unload(newDomain);
    }
    catch (Exception e)
    {
      Trace.WriteLine("APPLICATION LOADER: Failed to start application at:  " + 
        config.ExePath);
      HandleTerminalError(e);
    }
    

    Another way you could go about getting the desired effect would be to implement your configuration values inside a resource file compiled into each of your DLLs. A simple interface over the configuration object would allow you to switch out looking in an app.config versus looking in a resource file.

提交回复
热议问题