【问题标题】:How to call a dll created from .net core console app using .NET Core API如何使用 .NET Core API 调用从 .NET Core 控制台应用程序创建的 dll
【发布时间】:2020-01-25 06:48:18
【问题描述】:

我正在尝试通过从 .NET Core API 调用从 .NET Core (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());
        }
    }
}

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

【问题讨论】:

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

标签: c# asp.net-core-webapi asp.net-core-2.1


【解决方案1】:

我实际上犯了一个愚蠢的错误,在我的程序中添加了一个 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();
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2019-12-29
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多