很好的问候, 我尝试使用CSharp在asp.net上使用R脚本的结果。在开发中,该脚本可以正确响应,但是在IIS中发布时,它不会给出答案,也不会生成异常。
服务器已安装R,并且我需要运行脚本的库,因此在服务器上进行R学习的脚本测试结果符合预期。问题是必须运行脚本时IIS无法响应。
我的代码c#:
public static string RTypeCups()
{
try
{
string result = EXecutorScript.RunFromCmd(@"C:\projects\scriptsR\script2.R", @"C:\Program Files\R\R-3.6.1\bin\Rscript.exe", "3");
result = result.Replace("ISO8859 - 1", "utf-8");
return result;
}
catch (Exception e)
{
return e.Message;
}
}
public static string RunFromCmd(string rCodeFilePath, string rScriptExecutablePath, string args)
{
string file = rCodeFilePath;
string result = string.Empty;
try
{
var info = new ProcessStartInfo();
info.FileName = rScriptExecutablePath;
info.WorkingDirectory = Path.GetDirectoryName(rScriptExecutablePath);
info.Arguments = rCodeFilePath + " " + args;
info.RedirectStandardInput = false;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
using (var proc = new Process())
{
proc.StartInfo = info;
proc.Start();
result = proc.StandardOutput.ReadToEnd();
}
return result;
}
catch (Exception ex)
{
return "R Script failed: " + result + ex.ToString();
}
}