简体   繁体   中英

C# Project calling another Project failing with App.config

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM