Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Web App Temp file cleaning responsibility

In one of my Azure Web App Web API application, I am creating temp files using this code in a Get method

    string path = Path.GetTempFileName();
    // do some writing on this file. then read 
    var fileStream = File.OpenRead(path);
    // then returning this stream as a HttpResponseMessage response

My question is, in a managed environment like this (not in VM), do I need to clear those temporary files by myself? Shouldn't Azure itself supposed to clear those temp files?

like image 927
Foyzul Karim Avatar asked Dec 13 '15 03:12

Foyzul Karim


2 Answers

Those files only get cleaned when your site is restarted.

If your site is running in Free or Shared mode, it only gets 300MB for temp files, so you could run out if you don't clean up.

If your site is in Basic or Standard mode, then there is significantly more space (around 200GB!). So you could probably get away with not cleaning up without running into the limit. Eventually, your site will get restarted (e.g. during platform upgrade), so things will get cleaned up.

See this page for some additional detail on this topic.

like image 186
David Ebbo Avatar answered Sep 25 '22 07:09

David Ebbo


Maybey if you extend FileStream you can override dispose and remove it when disposed is called? That is how i'm resolving it for now. If i'm wrong let me know.

 /// <summary>
/// Create a temporary file and removes its when the stream is closed.
/// </summary>
internal class TemporaryFileStream : FileStream
{
    public TemporaryFileStream() : base(Path.GetTempFileName(), FileMode.Open)
    {
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        // After the stream is closed, remove the file.
        File.Delete(Name);
    }
}
like image 38
Michael Vonck Avatar answered Sep 26 '22 07:09

Michael Vonck