2017-04-04 28 views
0

我有一个将参数发送到R脚本的ASP.NET MVC应用程序,R脚本然后生成一个文件并将其放在本地文件夹中。该过程通过Visual Studio在我的本地计算机上完美工作,但在发布和使用IIS后进出。以下是我如何初始化R.NET并通过AJAX从我的视图中获取这些值。为什么R.NET在本地工作但在IIS中停止工作?

我也将这两个PATH放在我的系统环境变量中。

如果有人知道为什么IIS只有在重新启动操作系统后才能正常工作,然后在我非常感激之后不再工作。似乎很奇怪,我在Visual Studio中遇到了我在IIS中遇到的问题。

 [HttpGet] 
    public JsonResult Index1(string modelTypes = null, string fileNames = null, string[] locations = null, string lowerLimits = null, string upperLimits = null, string areas = null, string productivities = null, string[] fobs = null) 
    { 
     @ViewBag.UserName = System.Environment.UserName; 
     string userName = @ViewBag.UserName; 
     //Declare field parameters 
     var strModelTypeValue = modelTypes; 
     string strFileName = fileNames; 
     var strLocationValue = locations; 
     var strLowerLimitValue = lowerLimits; 
     var strUpperLimitValue = upperLimits; 
     string strAreaValue = areas; 
     string strProductivityValue = productivities; 
     string strFobValue = fobs.ToString(); 
     var libraryLocation = "C:/Users/" + userName + "/Documents/R/win-library/3.2"; 

     string rPath = @"C:\Program Files\R\R-3.3.2\bin\x64"; 
     string rHome = @"C:\Program Files\R\R-3.3.2"; 

     //Initialize REngine 
     REngine.SetEnvironmentVariables(rPath, rHome); 
     REngine engine = REngine.GetInstance(); 
     engine.Initialize(); 

     if (fobs.Length > 1) 
     { 
      strFobValue = string.Join(" ", fobs); 
     } 
     else 
     { 
      strFobValue = "ALL"; 
     } 




     //Declare R Script path 
     var rScriptPath = "C:/Users/" + userName + "/Documents/R/RWDir/Loop_Optimization.R"; 

     //Check to see if there was more than one location selected 
     if (strLocationValue.Length > 1) 
     { 

      foreach (var item in strLocationValue) 
      { 
       //Set string location to each location value in loop 
       var strlocation = item; 

       //Add values to parameter list 
       var myParams = new List<string> 
       {  

        strModelTypeValue, 
        strFileName,    
        strlocation,     
        strLowerLimitValue, 
        strUpperLimitValue, 
        strAreaValue, 
        strProductivityValue, 
        strFobValue, 
        libraryLocation 

       }; 

       //Set myParams as arguments to be sent to r script 
       engine.SetCommandLineArguments(myParams.ToArray()); 
       engine.Evaluate("source('" + rScriptPath + "')"); 
      } 

     } 
     //Only one location specified, no need to loop 
     else 
     { 
      foreach (var item in strLocationValue) 
      { 
       //Set string location to each location value in loop 
       var strlocation = item; 
       var myParams = new List<string> 
       { 

        strModelTypeValue, 
        strFileName, 
        strlocation, 
        strLowerLimitValue, 
        strUpperLimitValue, 
        strAreaValue, 
        strProductivityValue, 
        strFobValue, 
        libraryLocation 
       }; 

       engine.SetCommandLineArguments(myParams.ToArray()); 
       engine.Evaluate("source('" + rScriptPath + "')"); 
      } 
     } 
     //engine.Evaluate("source('" + rScriptPath + "')"); 
     //engine.Dispose(); 
     return Json("success", JsonRequestBehavior.AllowGet); 

    } 
+0

我的web应用程序有时在IIS上工作,但只有在OS重新启动后才会再次停止。 – Jwoody30

回答

-1

您是否通过附加过程来完成任何调试。

+0

我认为engine.Initialize();这是导致问题 –

+0

是否有替代engine.Initialize()以使用IIS?我现在将其重新创建为桌面应用程序,因为我在IIS中遇到了很多问题 – Jwoody30

相关问题