2

I am trying to manipulate my swagger doc. I need to run redoc split to separate the main swagger.

During start.cs I am getting the swagger doc and saving it to the file system. That works as expected. Then in my split method, I get the swagger doc and run it through redoc split and nothing happens, no error and no messages. I put breakpoints on the first line, inside the Try block and when I step, it exits so I think an error is getting swallowed somewhere.

I can run this from a command window just fine.

public static void RunSplitCommand()
{
    string output = string.Empty;
    string error = string.Empty;
    var args = "redocly split \"../bin/swagger/Swagger.json\"  --outDir= \"../bin/swagger\"";
    
    using (var process = new System.Diagnostics.Process())
    {
        process.StartInfo = new System.Diagnostics.ProcessStartInfo();
        process.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = args;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        
        try
        {
            process.Start();
        
            process.WaitForExit();
            if (process.StandardOutput.ReadToEnd() != string.Empty)
                output = process.StandardOutput.ReadToEnd();
    
            else if (process.StandardError.ReadToEnd() != string.Empty)
                output = process.StandardError.ReadToEnd();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
    
    string.Concat(output, "\n");
}
15
  • 1
    Just run process.StartInfo.FileName = "redocly.exe"; instead (and pass your args). Commented Mar 28 at 17:48
  • 4
    So what is redocly? A batch? Surely it's an actual application stored somewhere. You don't need cmd.exe to run something and get its std output. Any CLI application can be used. It's a common mistake to go via cmd.exe just because it's "command line something something" Commented Mar 28 at 18:08
  • 1
    You're calling process.StandardOutput.ReadToEnd() twice, without resetting the stream position. Either call process.StandardOutput.Seek(0, SeekOrigin.Begin) before the second call, or better use pattern matching: else if (process.StandardError.ReadToEnd() is { } errorString) { output = errorString; } Commented Mar 28 at 18:10
  • 1
    Use the fully-qualified filename. Commented Mar 28 at 18:18
  • 3
    If for some reason you need to use, cmd.exe, then see the following post: stackoverflow.com/a/66787416/10024425 Commented Mar 28 at 18:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.