简体   繁体   English

C#项目调用另一个项目失败,但App.config

[英]C# Project calling another Project failing with App.config

I have a solution with 2 projects. 我有2个项目的解决方案。 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. 我希望能够更改第一个项目生成的格式,所以我更改了它的静态格式字符串,以从App.Config加载。 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. 进行此更改之后,无论何时第二个项目创建第一个项目的进程,它都会失败,并出现ArgumentNullException异常,并且似乎不加载格式字符串。

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: 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 . config路径,如下所述: 使用ConfigurationManager从任意位置加载config 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. 不幸的是,这不能解决错误,因此我将app.config的副本移至两个项目中。 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. 我对其进行了测试,第二个项目从其自己的应用程序配置中成功加载,但是当它在第一个项目的过程中调用.Start()时,它仍然失败,并出现相同的ArgumentNullException错误。

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? 如果从项目2中调用Process 1,则似乎没有从配置文件中加载项目1。项目2可以看到它自己的配置文件,与项目1中的相同。这是否仍然是配置文件问题?还有什么吗

The App.config which is loaded is that of the project you run. 加载的App.config是您运行的项目的App.config the other project is effectively referenced as a class library and its App.config file will not be loaded. 另一个项目被有效地引用为类库,并且不会加载其App.config文件。 You'll need to place the setting in your first project's config file as well. 您还需要将设置放置在第一个项目的配置文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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