简体   繁体   中英

How to call a dll created from .net core console app using .NET Core API

I'm trying to start a process by calling a dll created from .NET Core (2.1) console app from .NET Core API. I tried this using creating a process. The following shows the code.

            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();
            }

And in the ReportGeneratorApp main method I'm trying to create a file in the file system.

    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());
        }
    }
}

If I run the ReportGeneratorApp from cmd it does work. But not when i call it from web api. Any clues?

I actually made a silly mistake by addding a Console.ReadLine() in my program. So it won't run from that point onwards. I had to remove it in order to work.

Also I made some change in the code where i call the process to start. I need to call the waitforexit() method until I receive the response from the program.

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();
            }

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