我正在运行一个使用GRPC工具从原始文件生成c#类的过程。该代码在我的开发计算机上工作正常,但是一旦部署在azure上,该过程就会运行,但会返回ExitCode = -1073740791
并且输出为字符串空。
我已经记录了我执行的命令,如果我尝试在Kudu
控制台中执行该命令,它将正常工作,并且会按预期生成c#文件。
命令如下:
Tools\bin\tools\windows_x86\protoc.exe
-ID:\local\LocalAppData\GrpcTester\0969261f-bfa7-45a8-bba1-7e1ccd3818e7\protos
--csharp_out=D:\local\LocalAppData\GrpcTester\0969261f-bfa7-45a8-bba1-7e1ccd3818e7\csharpfiles
--grpc_out=D:\local\LocalAppData\GrpcTester\0969261f-bfa7-45a8-bba1-7e1ccd3818e7\csharpfiles
--plugin=protoc-gen-grpc="Tools\bin\tools\windows_x86\grpc_csharp_plugin.exe"
D:\local\LocalAppData\GrpcTester\0969261f-bfa7-45a8-bba1-7e1ccd3818e7\protos\mobile_backend.proto
private bool GenerateGRPCCSharpFiles(TestProject testProject)
{
bool succes = false;
var outputFolder = WorkspaceManager.GetProjectSubFolder(testProject.Id.ToString(), WorkspaceManager.CSharpFilesPath);
var protoFilesFolder = WorkspaceManager.GetProjectSubFolder(testProject.Id.ToString(), WorkspaceManager.ProtosPath);
// Install tools
RequireTools().Wait();
var protoc = ProtocPath();
var plugin = ProtocPluginPath();
logger.LogDebug($"GenerateGRPCCSharpFiles : protoc: {protoc}");
logger.LogDebug($"GenerateGRPCCSharpFiles : plugin: {plugin}");
//string protocol = Path.Combine(protoFilesFolder, protoFiles.FirstOrDefault().FileName);
string protocol = Directory.GetFiles(protoFilesFolder).FirstOrDefault();
var command = new string[]
{
$"-I{protoFilesFolder}",
$"--csharp_out={outputFolder}",
$"--grpc_out={outputFolder}",
$"--plugin=protoc-gen-grpc=\"{plugin}\"",
protocol,
};
try
{
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = protoc;
process.StartInfo.Arguments = string.Join(' ', command);
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit();
using (var streamReader = new StreamReader(process.StandardOutput.BaseStream))
{
var response = streamReader.ReadToEnd();
logger.LogDebug($"GenerateGRPCCSharpFiles: Completed output: {response.ToString()}");
}
succes = process.ExitCode == 0;
};
}
catch (Exception ex)
{
throw ex;
}
return succes;
}
答案 0 :(得分:0)
答案 1 :(得分:0)
它与必须在WaitForExit之前执行的RedirectStandardOutput(愚蠢的错误)有关。查看修改后的代码:
process.StartInfo.FileName = protoc;
process.StartInfo.Arguments = string.Join(' ', command);
//You must set UseShellExecute to false if you want to set RedirectStandardOutput to true.
//Otherwise, reading from the StandardOutput stream throws an exception.
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
string eOut = null;
process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => { eOut += e.Data; });
process.Start();
// To avoid deadlocks, always read the output stream first and then wait.
// To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
process.BeginErrorReadLine();
string output = process.StandardOutput.ReadToEnd();
logger.LogDebug($"GenerateGRPCCSharpFiles: Completed output: {output.ToString()}");
process.WaitForExit();