Search code examples
azure-functions.net-6.0

Clean temp path on Azure Function .NET 6


I'm using an Azure Function that needs to render a pdf, get the first page of it and convert it to a png file. For do this, I need to save a temporary file in storage using Path.GetTempPath(). The temporal file needs a predefined structure.

 var randomTempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
 var tmpPngImages = Path.Combine(randomTempPath, "images*.png");  

And finally, at the end of function I clen the temporal data.

if (File.Exists(tmpPngImages))
  {
    File.Delete(tmpPngImages);
  }

The question is, when the function ends, I really need to remove this temporary file or the Garbage collector does this task for me?

I don't know if the process to clean the temporal file cleans the correct file or cleans another file. At the same time, if the Garbage Collector does this for me, I can avoid to use this code.


Solution

  • The question is, when the function ends, I really need to remove this temporary file or the Garbage collector does this task for me?

    You need to remove this temporary file as garbage collector will not remove it.

    Firstly just created a temp file by using below code:

    using System.Net;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Azure.Functions.Worker.Http;
    using Microsoft.Extensions.Logging;
    
    namespace FunctionApp32
    {
        public class Function1
        {
            private readonly ILogger _logger;
    
            public Function1(ILoggerFactory loggerFactory)
            {
                _logger = loggerFactory.CreateLogger<Function1>();
            }
    
            [Function("Function1")]
            public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
            {
                _logger.LogInformation("C# HTTP trigger function processed a request.");
    
                var response = req.CreateResponse(HttpStatusCode.OK);
                response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
                string t = Path.GetTempFileName();
                Console.WriteLine(t);
                using (StreamWriter writer = new StreamWriter(t))
                {
                    await writer.WriteLineAsync("Hello, temporary file!");
                }
                response.WriteString("Temp Created");
    
                return response;
            }
        }
    }
    

    Then Outputs are:

    enter image description here

    Went and checked the location after exceuting the function:

    enter image description here

    It still exists.

    For that you need to delete it as i have done below:

        using System.Net;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Azure.Functions.Worker.Http;
    using Microsoft.Extensions.Logging;
    
    namespace FunctionApp32
    {
        public class Function1
        {
            private readonly ILogger _logger;
    
            public Function1(ILoggerFactory loggerFactory)
            {
                _logger = loggerFactory.CreateLogger<Function1>();
            }
    
            [Function("Function1")]
            public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
            {
                _logger.LogInformation("C# HTTP trigger function processed a request.");
    
                var response = req.CreateResponse(HttpStatusCode.OK);
                response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
                string t = Path.GetTempFileName();
                Console.WriteLine(t);
                using (StreamWriter writer = new StreamWriter(t))
                {
                    await writer.WriteLineAsync("Hello, temporary file!");
                }
                Console.WriteLine("Temp Created");
                File.Delete(t);
                Console.WriteLine("Temp Deleted");
                response.WriteString("Temp Created and deleted");
                return response;
            }
        }
    }
    

    Then outputs:

    enter image description here

    Now checked in Location:

    enter image description here

    Now its deleted.

    You need to delete it as I have done.