Click or drag to resize

Set host configuration file

Problem

You want to set the host configuration file.

Solution

To set the configuration file, call SetConfigurationFile and pass the path of the configuration file.

C#
using System;
using System.Configuration;
using System.IO;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // For this example, let's just create the example config file.  In production,
            // you may know beforehand your configuration file or if you want the host
            // process to use the same configuration file the current application is running
            // as you can pass: AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
            string configurationPath = Path.GetTempFileName();
            File.WriteAllText(configurationPath, @"<configuration>
  <appSettings>
    <add key=""YourKey"" value=""KeyValue"" />
  </appSettings>
</configuration>");

            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();
                Job job = scheduler.CreateJob();

                // Pass in the configuration file path to SetConfigurationFile
                // You can also pass the configuration content as a string to SetConfigurationString
                job.TaskEnvironment.SetConfigurationFile(configurationPath);

                job.AddTask(new ReportsConfigurationFile());
                job.Submit();
                job.WaitUntilDone();

                Console.WriteLine("YourKey: " + job.Tasks[0].Result);
                Console.WriteLine("ConfigurationContent:\n" + job.Tasks[0].StandardOutput);
            }

            /*
             * The output of the application should resemble:
             * YourKey: KeyValue
             * ConfigurationContent:
             * <configuration>
             *   <appSettings>
             *     <add key="YourKey" value="KeyValue" />
             *   </appSettings>
             * </configuration>
             */
        }

        [Serializable]
        class ReportsConfigurationFile : Task
        {
            public override void Execute()
            {
                // Demonstrates how you could use the example configuration content we sent.

                // If you receive "error CS0103: The name 'ConfigurationManager' does not exist in the current context"
                // at this line, add a reference to the System.Configuration assembly to the project.
                this.Result = ConfigurationManager.AppSettings["YourKey"].ToString();

                // Let's just print out the file so that we see that it is actually loaded on the host side.
                Console.WriteLine(File.ReadAllText(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
            }
        }
    }
}
Discussion

The configuration file for the application domain of a host executing a task can be changed. For example, an application domain should be configured with a modified configuration file if it requires specific assembly search rules or custom configuration data.

Note: Due to behavior differences of AppDomain and ConfigurationManager in .NET 5, the solution presented above only works when using .NET Framework.

See Also

Reference

ConfigurationFile

STK Parallel Computing Server 2.9 API for .NET