Last active
February 8, 2021 18:31
-
-
Save WadeTheFade/c2958e465ab7b3a5a0a109ef255144c2 to your computer and use it in GitHub Desktop.
List being overwritten by last value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sentFiles = new List<FileTrack>(); | |
foreach (var destination in model.Destinations) | |
{ | |
sentFiles.AddRange(_mover(destination.LocationType).ToDestination(destination, processLog, fileTracker, model.StagingLocation)); | |
} | |
//EXPECTED VALUES HAVE BEEN OVERWROTE WITH LAST VALUE ADDED VIA ADD() | |
fileTracker.Sent = sentFiles; | |
return fileTracker; | |
public List<FileTrack> ToDestination(Destination destination, ProcessLog processLog, FileTracker files, string stagingLocation) | |
{ | |
var sentFiles = new List<FileTrack>(); | |
_logger.LogInformation($"Destination START {destination.LocationType} thread Id: {Thread.CurrentThread.ManagedThreadId}"); | |
foreach (var file in files.Outgoing) | |
{ | |
var stagingFilePath = $"{stagingLocation}\\{file.FileName}.{file.Ext}"; | |
using var staging = new FileStream(stagingFilePath, FileMode.Open); | |
_logger.LogInformation($"Destination OPEN FILE STREAM {destination.LocationType} thread Id: {Thread.CurrentThread.ManagedThreadId}"); | |
var inputFile = new FileInfo(stagingFilePath); | |
var addedFile = new Core.Domain.Entities.File() | |
{ | |
Name = Path.GetFileNameWithoutExtension(inputFile.Name), | |
Length = inputFile.Length, | |
Extension = Path.GetExtension(inputFile.Name).TrimStart('.'), | |
FullPath = inputFile.FullName, | |
ProcessLog = processLog | |
}; | |
_db.Files.Attach(addedFile); | |
var fileTransferLog = new FileTransferLog() { File = addedFile, LocationId = destination.LocationId }; | |
//FileTransferLog Start | |
_db.FileTransferLogs.Add(fileTransferLog); | |
_db.SaveChanges(); | |
_logger.LogInformation($"Destination SAVE {destination.LocationType} thread Id: {Thread.CurrentThread.ManagedThreadId}"); | |
var destinationPath = $"{destination.Location}\\{file.FileName}.{file.Ext}"; | |
_logger.LogInformation($"Impersonating: {destination.Account.Domain}\\{destination.Account.Username}"); | |
_policies.NetworkImpersonationPolicy.Execute(() => | |
{ | |
//NO ASYNC ZONE IMPERSONATION IN FULL AFFECT | |
Impersonation.RunAsUser(new UserCredentials(destination.Account.Domain, destination.Account.Username, destination.Account.Password), LogonType.NewCredentials, () => | |
{ | |
_logger.LogInformation($"Transferring: {addedFile.FileId} {inputFile.Name}"); | |
using FileStream output = new FileStream(destinationPath, destination.Options.Overwrite ? FileMode.Create : FileMode.CreateNew, FileAccess.Write); | |
Directory.CreateDirectory(destination.Location); | |
staging.CopyTo(output); | |
_logger.LogInformation($"Transferred: {addedFile.FileId} {inputFile.Name}"); | |
_logger.LogInformation($"Destination IMPERSONATION {destination.LocationType} thread Id: {Thread.CurrentThread.ManagedThreadId}"); | |
}); | |
//NO ASYNC ZONE IMPERSONATION IN FULL AFFECT | |
}); | |
//FileTransferLog End | |
fileTransferLog.EndTransferDate = DateTime.UtcNow; | |
_db.SaveChanges(); | |
_logger.LogInformation($"Destination SAVE {destination.LocationType} thread Id: {Thread.CurrentThread.ManagedThreadId}"); | |
sentFiles.Add(new FileTrack | |
{ | |
FileId = addedFile.FileId, | |
FullPath = destinationPath, | |
FileName = addedFile.Name, | |
Ext = addedFile.Extension | |
}); | |
} | |
_logger.LogInformation(JsonConvert.SerializeObject(sentFiles, Formatting.Indented)); | |
return sentFiles; | |
} |
Author
WadeTheFade
commented
Feb 8, 2021
EXPECTED RESULTS:
[
{
"FileId": "026f3722-2f6a-eb11-b8a9-4c1d968ab2e4",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "\\\\***Test\\IPAutomaion_Views.zip"
},
{
"FileId": "006f3722-2f6a-eb11-b8a9-4c1d968ab2e4",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "\\\\***Test2\\IPAutomaion_Views.zip"
}
]
ACTUAL:
[
{
"FileId": "026f3722-2f6a-eb11-b8a9-4c1d968ab2e4",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "\\\\***Test\\IPAutomaion_Views.zip"
},
{
"FileId": "026f3722-2f6a-eb11-b8a9-4c1d968ab2e4",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "\\\\***Test\\IPAutomaion_Views.zip"
}
]
Last element in list, is overwriting all values in list.
Output BEFORE .add() shows the correct data:
[
{
"FileId": "006f3722-2f6a-eb11-b8a9-4c1d968ab2e4",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "\\\\***Test2\\IPAutomaion_Views.zip"
}
]
and
[
{
"FileId": "026f3722-2f6a-eb11-b8a9-4c1d968ab2e4",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "\\\\***Test\\IPAutomaion_Views.zip"
}
]
FIRST PASS:
BEGINNING FILETRACKER [
{
"FileId": "00000000-0000-0000-0000-000000000000",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "C:\\TestData\\StagingLocation\\076181fc-376a-eb11-b8a9-4c1d968ab2e4\\IPAutomaion_Views.zip"
}
]
SECOND PASS:
BEGINNING FILETRACKER [
{
"FileId": "21dada02-386a-eb11-b8a9-4c1d968ab2e4",
"FileName": "IPAutomaion_Views",
"Ext": "zip",
"FullPath": "\\\\XRPSWDAPPBIZ05E.hcadev.corpaddev.net\\Integration\\LOBUpgrade\\IPATest2\\IPAutomaion_Views.zip"
}
]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment