private void readLocalFiles(FtpService ftpManager) { DirectoryInfo d = new DirectoryInfo(leadAdapterAndAccountMap.LocalFilePath); FileInfo[] Files = d.GetFiles("*.xml"); if (Files != null && Files.Count() > 0) { var response = customFieldService.GetAllCustomFields(new GetAllCustomFieldsRequest(leadAdapterAndAccountMap.AccountID)); IEnumerable <FieldViewModel> customFields = response.CustomFields; var dropdownfeildsresposne = cacheService.GetDropdownValues(leadAdapterAndAccountMap.AccountID); IEnumerable <DropdownValueViewModel> phoneFields = dropdownfeildsresposne.Where(x => x.DropdownID == (short)DropdownFieldTypes.PhoneNumberType).Select(x => x.DropdownValuesList).FirstOrDefault(); Logger.Current.Informational("Processing files for account : " + leadAdapterAndAccountMap.AccountName + " count of files : " + Files.LongLength); foreach (var file in Files) { Logger.Current.Informational("Current Processed File Name: " + file.Name); var localFilePath = Path.Combine(leadAdapterAndAccountMap.LocalFilePath, file.Name); try { int jobid = Convert.ToInt32(Path.GetFileNameWithoutExtension(localFilePath).Split('~')[1]); var fileName = Path.GetFileNameWithoutExtension(localFilePath).Split('~')[0] + Path.GetExtension(localFilePath); /* Get contacts */ var contacts = new ImportContactsData(); contacts = GetContacts(localFilePath, customFields, jobid, phoneFields); if (contacts.ContactData != null && contacts.ContactData.Any()) { Logger.Current.Informational("Got contacts for inserting : " + contacts.ContactData.Count); /* Bulk Insert */ Task.Factory.StartNew(() => { MailgunVerification(contacts.ContactData.ToList()); }, TaskCreationOptions.LongRunning); Task.Factory.StartNew(() => { ContactsBulkinsert(contacts); }, TaskCreationOptions.LongRunning); } else { Logger.Current.Informational("No contacts for inserting, Account name : " + leadAdapterAndAccountMap.AccountName); } /* File move to archive after processing */ var fileExtension = Path.GetExtension(localFilePath); var newFileName = Guid.NewGuid() + fileExtension; importDataRepository.UpdateLeadAdapterJobLogsWithProcessedFileName(jobid, newFileName); string archivedfile = string.Empty; if (Directory.Exists(leadAdapterAndAccountMap.ArchivePath) && System.IO.File.Exists(localFilePath)) { archivedfile = Path.Combine(leadAdapterAndAccountMap.ArchivePath, newFileName); File.Move(localFilePath, archivedfile); } else if (File.Exists(localFilePath)) { File.Delete(localFilePath); } bool isFolderExist = ftpManager.CreateFTPDirectory(leadAdapterAndAccountMap.RequestGuid); if (isFolderExist) { ftpManager.MoveFile(leadAdapterAndAccountMap.RequestGuid, fileName, archivedfile, jobid); } else { ftpManager.Delete(leadAdapterAndAccountMap.RequestGuid, string.Empty, new List <string> { fileName }); } } catch (IndexOutOfRangeException indexExc) { Logger.Current.Error("Index out of range exception while accessing jobid and filename, account name : " + leadAdapterAndAccountMap.AccountName, indexExc); var fileExtension = Path.GetExtension(localFilePath); var newFileName = Guid.NewGuid() + fileExtension; string archivedfile = string.Empty; archivedfile = Path.Combine(leadAdapterAndAccountMap.ArchivePath, newFileName); File.Move(localFilePath, archivedfile); //These are old files continue; } } Logger.Current.Informational("Files processed succcessfully for account : " + leadAdapterAndAccountMap.AccountName + " count of files : " + Files.LongLength); } }