2014-10-28 4 views
0

MVC 5 프로젝트에서 일하고 있습니다. 약 100 개의 파일을 구문 분석하고 구문 분석 된 데이터를 데이터베이스에 저장해야합니다. 파일이 FTP 서버에서 전송됩니다. 그 복용 긴에 - -다중 FTP 다운로드

지금이

public ActionResult ReadAll() 
    { 
     // Get all installations 
     var installations = db.Installations.ToList(); 

     // if installation unknown --> read installation 
     foreach (var installation in installations) 
     { 

      var path = string.Format(@"{0}/{1}", installation.FtpMap, "file"); 
      string fileToString = ftp.Download(path); 
      var parser = new ParseService(); 
      parser.ParseStringFile(fileToString, installation); 
      db.Entry(installation).State = EntityState.Modified; 
      db.SaveChanges(); 

     } 
     return RedirectToAction("Index"); 
    } 

public class FtpService 
{ 
    private const string Host = @"host"; 

    private readonly string user; 
    private readonly string pass; 

    private FtpWebRequest ftpRequest; 
    private FtpWebResponse ftpResponse; 


    public FtpService(string userName, string password) 
    { 
     user = userName; 
     pass = password; 
    } 

    /* Download File */ 
    public string Download(string remoteFile) 
    { 
     try 
     { 
      string ftpfullpath = string.Format("ftp://{0}/{1}.js", Host, remoteFile); 

      ftpRequest = (FtpWebRequest)WebRequest.Create(ftpfullpath); 
      ftpRequest.Credentials = new NetworkCredential(user, pass);     
      ftpRequest.UseBinary = true; 
      ftpRequest.UsePassive = true; 
      ftpRequest.KeepAlive = true; 
      ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
      ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 

      var ftpStream = ftpResponse.GetResponseStream(); 
      if (ftpStream == null) 
      { 
       throw new WebException(); 
      } 

      var streamReader = new StreamReader(ftpStream); 
      var text = streamReader.ReadToEnd(); 
      streamReader.Close(); 
      ftpStream.Close(); 
      ftpResponse.Close(); 
      ftpRequest = null; 
      return text; 
     } 
     catch (Exception ex) { Debug.WriteLine(ex.ToString()); } 
     return null; 
    } 


public class ParseService 
{ 
    private InstallationContext db = new InstallationContext(); 

    public void ParseStringFile(string fileAsText, Installation installation) 
    { 
     using (var reader = new StringReader(fileAsText)) 
     { 
      // Loop over the lines in the string. 
      string line; 
      while ((line = reader.ReadLine()) != null) 
      { 
       // do some parsing with reflection 
       Debug.WriteLine(line); 
      } 
     } 
    } 

문제처럼되어하고있어 사람이 나에게 코드 (작업 작업? 큐의 일종을 개선하기 위해 몇 가지 지침을 줄 수 있습니까?) 사전에 감사

+0

ocessed http://signalr.net/ –

+0

대부분의 ftp 서버는 다중 스레드를 고려하고있는 경우 동시 연결의 양을 제한합니다. – Silvermind

답변

0

이 시도 :

Parallel.Foreach (installations,()=> 
    { 

     var path = string.Format(@"{0}/{1}", installation.FtpMap, "file"); 
     string fileToString = ftp.Download(path); 
     var parser = new ParseService(); 
     parser.ParseStringFile(fileToString, installation); 
     db.Entry(installation).State = EntityState.Modified; 
     db.SaveChanges(); 

    } 
당신은 SignalR을 사용하여 웹 UI에 다시보고하고 파일이 홍보가로 통지 다른 프로세스를 시작할 수