2017-04-04 52 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

相關問題