1

I have a web app developed in ASP.Net MVC 5 hosted in Azure. I am using a shared app service, not VMs. Recently Azure has started showing warnings that I need to reduce my app's usage of temporary files on workers.

Temp file utilization

After restarting the app, the problem has gone away. Seems that temporary apps were cleared by doing a restart.

How to detect and prevent unexpected growth of the temporary file usages. I am not sure what generated 20 GB of temporary files. What should I look for reduce app usage of temporary? I am not explicitly storing anything in temporary files in code, data is stored in the database, so not sure what to look for?

What are the best practices that should be followed in order to keep the Temp File usages in a healthy state and prevent any unexpected growth?

Note: I have multiple virtual path with same physical path in my Web App.

Virtual path

try
{
if (file != null && file.ContentLength > 0)
{
    var fileName = uniqefilename;

    CloudStorageAccount storageAccount = AzureBlobStorageModel.GetConnectionString();

    if (storageAccount != null)
    {
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        string containerName = "storagecontainer";

        CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        bool isContainerCreated = container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);                                

        CloudBlobDirectory folder = container.GetDirectoryReference("employee");

        CloudBlockBlob blockBlob = folder.GetBlockBlobReference(fileName);

        UploadDirectory = String.Format("~/upload/{0}/", "blobfloder");
        physicalPath = HttpContext.Server.MapPath(UploadDirectory + fileName);
        file.SaveAs(physicalPath);
        isValid = IsFileValid(ext, physicalPath);
        if (isValid)
        {
            using (var fileStream = System.IO.File.OpenRead(physicalPath))
            {                                        
                blockBlob.Properties.ContentType = file.ContentType;
                blockBlob.UploadFromFile(physicalPath);
                if (blockBlob.Properties.Length >= 0)
                {
                    docURL = blockBlob.SnapshotQualifiedUri.ToString();
                    IsExternalStorage = true;
                    System.Threading.Tasks.Task T = new System.Threading.Tasks.Task(() => deletefile(physicalPath));
                    T.Start();
                }
            }
        }
    }
 }
}
catch (Exception ex)
{

}


//Delete File 
public void deletefile(string filepath)
{
  try
  {
    if (!string.IsNullOrWhiteSpace(filepath))
    {
        System.GC.Collect();
        System.GC.WaitForPendingFinalizers();
        System.IO.File.Delete(filepath);
    }
  }
  catch(Exception e) { }
 }
Karthy
  • 11
  • 2

1 Answers1

0

You problem may be caused by using temporary files to process uploads or downloads. The solution would be either to process files using memory stream instead of filestream or delete the temporary files after you are finished processing. This SO exchange has some relevant suggestions: Azure Web App Temp file cleaning responsibility

Given your update, it looks like your file upload code lets temp files accumulate in line 39, because you are not waiting for your async call to delete the file to finish before you exit. I assume that this code block is tucked inside an MVC controller action, which means that, as soon as the code block is finished, it will abandon the un-awaited async action, leaving you with an undeleted temp file.

Consider updating your code to await your Task action. Also, you may want to update to Task.Run. E.g.,

var t = await Task.Run(async delegate
{
    //perform your deletion in here
    return some-value-if-you-want;
});
longestwayround
  • 979
  • 9
  • 13
  • @@longestwayround As per your comment, I updated the question and added image (Uploading file code) for your reference. Can you please check it and let me know if anything making us issues... – Karthy Jun 09 '20 at 13:49