C# 如何使用.net core API调用从.net core控制台应用程序创建的dll

C# 如何使用.net core API调用从.net core控制台应用程序创建的dll,c#,asp.net-core-webapi,asp.net-core-2.1,C#,Asp.net Core Webapi,Asp.net Core 2.1,我试图通过从.NETCoreAPI调用从.NETCore(2.1)控制台应用程序创建的dll来启动一个进程。我尝试了创建一个流程。下面显示了代码 string filePath = @"C:\Projects\MyProject.Api\bin\Debug\netcoreapp2.1\"; var startInfo = new ProcessStartInfo { FileName =

我试图通过从.NETCoreAPI调用从.NETCore(2.1)控制台应用程序创建的dll来启动一个进程。我尝试了创建一个流程。下面显示了代码

            string filePath = @"C:\Projects\MyProject.Api\bin\Debug\netcoreapp2.1\"; 

            var startInfo = new ProcessStartInfo
            {
                FileName = "dotnet",
                WorkingDirectory = filePath,
                Arguments = "ReportGeneratorApp.dll",
                UseShellExecute = false,
                RedirectStandardOutput = false,
                RedirectStandardError = false,
                CreateNoWindow = true,
            };

            using (Process process = new Process())
            {
                process.StartInfo = startInfo;
                process.Start(); // process.WaitForExit();
            }
在ReportGeneratorApp主方法中,我尝试在文件系统中创建一个文件

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        for (int i = 0; i < args.Length; i++)
        {
            Console.WriteLine(args[i]);
        }

        string path = @"D:\MyTest.txt";
        Console.ReadLine();
        try
        {

            // Delete the file if it exists.
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            // Create the file.
            using (FileStream fs = File.Create(path))
            {
                byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file." + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss"));
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }

            // Open the stream and read it back.
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}
static void Main(字符串[]args)
{
控制台。WriteLine(“你好,世界!”);
for(int i=0;i

如果我从cmd运行ReportGeneratorApp,它确实可以工作。但当我从WebAPI调用它时就不是了。有什么线索吗?

我实际上犯了一个愚蠢的错误,在程序中添加了Console.ReadLine()。所以它不会从那一点开始运行。我必须把它取下来才能工作

我还在调用流程开始的代码中做了一些更改。我需要调用waitforexit()方法,直到收到程序的响应

var startInfo = new ProcessStartInfo
            {
                FileName = "dotnet",
                WorkingDirectory = fileDirectoryPath,
                Arguments = "ReportGeneratorApp.dll",
                RedirectStandardOutput = true
            };

            using (Process process = new Process())
            {
                process.StartInfo = startInfo;

                process.Start();
                process.WaitForExit();

                var output = await process.StandardOutput.ReadToEndAsync();
            }

Web api正在IIS或其他托管服务上运行。通常,那些托管应用程序会以尽可能低的权限运行您的web API和应用程序。所以,IIS用户可能只是缺少写入该D驱动器的访问权限。