0

I have a solution with 2 projects. One project is a console app that you plug information into and it will provide data back. The second project creates multiple processes that call the first project by passing an argument. Up to this point everything is working.

I wanted to play around with being able to change the format the first project generates so I changed it's static format string to load from App.Config instead. When I run the first project by itself there is no issue and everything still works. After this change whenever the second project creates a process of the first project it fails with an ArgumentNullException and seems to not load the format string.

Here is my code for the second project that is calling the console application.

    public static void Main(string[] args)
    {
        for (int i = 1; i < 10; i++)
        {
            Process EulerProblemN = CreateNewProcess(ExecutionPath, i.ToString());
            EulerProblemN.Start();
            string output = EulerProblemN.StandardOutput.ReadToEnd().Replace(Environment.NewLine, "");
            Console.WriteLine(output);
            EulerProblemN.WaitForExit();
        }
        Console.Read();
    }

    public static Process CreateNewProcess (string path, string argument)
    {
        Process eulersOutput = new Process();
        eulersOutput.StartInfo.FileName = path;
        eulersOutput.StartInfo.Arguments = argument;
        eulersOutput.StartInfo.RedirectStandardOutput = true;
        eulersOutput.StartInfo.UseShellExecute = false;
        return eulersOutput;
    }

In case it's relevant this is how I'm accessing the configuration in the first project:

private string AnswerFormat = ConfigurationManager.AppSettings["AnswerFormat"];

Contents of App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="AnswerFormat" value="The answer to Problem {0} is {1}, the program took: {2} milliseconds to complete."/>
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

What is cause of this error? Does it have to do with how the second project calls the first?

EDIT: First I tried to override the default env. config path as described here: Using ConfigurationManager to load config from an arbitrary location. I added that code to both projects. Unfortunately this didn't resolve the error so I moved a copy of app.config into both projects. The First project continues to run with no issues. I tested it and the second project successfully loads from its own app configuration, but when it calls .Start() on the process of the first project it is still failing with the same ArgumentNullException error.

It seems like Project 1 is not loading from the configuration file if called as a Process from project 2. Project 2 can see it's own config file and it's identical to the one in Project 1. Is this still a configuration file issue or could it be something else?

Community
  • 1
  • 1
wentimo
  • 471
  • 3
  • 12

1 Answers1

3

The App.config which is loaded is that of the project you run. the other project is effectively referenced as a class library and its App.config file will not be loaded. You'll need to place the setting in your first project's config file as well.

Haney
  • 32,775
  • 8
  • 59
  • 68
  • App.Config is shared by both projects, it already has access to it – wentimo Dec 09 '15 at 21:16
  • @Jesse Carter http://stackoverflow.com/questions/1361913/single-app-config-multi-project-c-sharp – wentimo Dec 09 '15 at 21:17
  • @wentimo that doesn't change the answer. The second application cannot access that at runtime like that. That SO question is more if you want to load any of those projects as the startup project. To do *that*, look at this question: http://stackoverflow.com/questions/4738/using-configurationmanager-to-load-config-from-an-arbitrary-location – ragerory Dec 09 '15 at 21:20
  • I moved a copy of app.config into the second project and I am still getting the same error – wentimo Dec 09 '15 at 22:19
  • 1
    Oh, you're newing up another *process*? It won't have any knowledge of the existing Process, including `App.config`. Why not reference the code via project reference instead of creating another process? Then AppSettings will be available. – Haney Dec 09 '15 at 22:30
  • @Haney how would I do that with a console application? – wentimo Dec 09 '15 at 23:01
  • @Haney I got it figured out. Thanks for pointing out the actual problem. – wentimo Dec 09 '15 at 23:11