I have some code that fetches files from Google Cloud Storage. The code works fine when run on my local development machine but when deployed to our production server it always "stops" whenever the parameter "objectsInBucket" (in the code below) is used.
In the example below the last code that actually executes correctly when deployed to the server is the line "LogHelper.LogToConsole("-6").
If I "uncomment" the foreach loop the last output to console is "- 4". Also, if I for example make a variable like "var count = objectsInBucket.Count();" and put it immediately after "objectsInBucket = storageClient.Listobjects(_gcsBucketName);" then the last output will be "- 1".
So basically, if I operate any method on the "objectsInBucket" it causes a Thread Cancel. Surrounding that piece of code with a try/catch does not make the code go into the catch either...
I've also tried fiddler to see if there are any outbound calls made when performing a method on the object but I have not managed to see any outbound calls being made.
But like I said, this problem only occurs when deployed to the server. So what can the cause of this be? Sorry for all the extensive calls to "LogHelper" in the provided code but they are there to show how far the code executes..
private void DownloadResourceFromGCS(string attachmentId)
{
LogHelper.LogToConsole($" - DownloadResourceFromGCS({attachmentId})");
using (StorageClient storageClient = StorageClient.Create())
{
Google.Api.Gax.PagedEnumerable<Google.Apis.Storage.v1.Data.Objects, Google.Apis.Storage.v1.Data.Object> objectsInBucket = null;
LogHelper.LogToConsole($" - 1");
objectsInBucket = storageClient.ListObjects(_gcsBucketName);
LogHelper.LogToConsole($" - 2 {JsonConvert.SerializeObject( objectsInBucket)}");
var dirPath = Path.Combine(_gcsAttachmentPath, attachmentId);
LogHelper.LogToConsole($" - 3");
if (objectsInBucket != null)
{
LogHelper.LogToConsole($" - 4");
var directoryInfo = new DirectoryInfo(dirPath);
if (directoryInfo.Exists)
{
LogHelper.LogToConsole($" - Deleting directory: {dirPath}");
directoryInfo.Delete(true);
}
directoryInfo.Create();
LogHelper.LogToConsole($" - Directory created: {dirPath}");
}
LogHelper.LogToConsole($" - 6");
IEnumerable<Google.Apis.Storage.v1.Data.Object> attachmentFiles = null;
LogHelper.LogToConsole($" - 7");
attachmentFiles = objectsInBucket.ToList().Where(x => x.Name.Contains(attachmentId)).ToList();
LogHelper.LogToConsole($" - 8");
LogHelper.LogToConsole($" - {attachmentFiles.Count()} attachments found where name matches attachmentid {attachmentId}");
LogHelper.LogToConsole($" - 9");
foreach (var attachmentFile in attachmentFiles)
//foreach (var attachmentFile in objectsInBucket)
{
LogHelper.LogToConsole($" - Processing attachment files");
var extension = Path.GetExtension(attachmentFile.Name);
// files/attachments may exists in the folder, but the folder name is also download as an object, but we don't need
if (!string.IsNullOrWhiteSpace(extension) && attachmentFile.Name.Contains(attachmentId))
{
var fileNames = attachmentFile.Name.Split('/');
var fileName = fileNames[1];
var filePath = Path.Combine(dirPath, fileName);
using (var outputFile = File.OpenWrite(filePath))
{
LogHelper.LogToConsole($" - Downloading attachment with name {fileName}");
try
{
storageClient.DownloadObject(_gcsBucketName, attachmentFile.Name, outputFile);
LogHelper.LogToConsole($" - Download of attachment with name {fileName} completed");
}
catch (Exception e)
{
LogHelper.LogToConsole($" - Failed to download attachment with name {fileName}");
throw;
}
}
}
}
}
LogHelper.LogToConsole($" - DownloadResourceFromGCS({attachmentId}) COMPLETED");
}