我正在开发一个MVC 5项目。 我需要解析大约100个文件并将解析后的数据保存到数据库中 文件来自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);
}
}
}
问题是 - 它需要很长时间 - 任何人都可以给我一些改进代码的指导(使用Tasks?,某种队列?) 提前致谢
答案 0 :(得分: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();
}