0

Every 24 hours, my code auto-generates a .csv-file, writes it temporarily to an Azure directory, and finally deletes it after operating on it.

It succeeds, but from the logs I can see, that an exception is thrown.

Exception:

"The process cannot access the file 'D:\home\site\wwwroot\myFile.csv' because it is being used by another process."

The log points to these two lines of code, where I simply specify the directory and file-name and then start a StreamWriter:

string filePath = Environment.CurrentDirectory + "\\myFile.csv"; //Specify where to create csv on hosted Azure server (not locally)
using (var w = new StreamWriter(filePath, false, new UTF8Encoding(false))) //Exception is thrown here
{
    //more code
}

I am very confused, how the two above lines can result in that exception, especially since the file is always deleted after upload.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
mnc
  • 388
  • 1
  • 2
  • 11
  • 2
    2 processes are accessing the same file, its as simple as that – TheGeneral Nov 11 '21 at 08:03
  • You need to trust me when I tell you this, there ***is*** more than one process trying to access (or write) to the file at once (even if you think it is a new file). Note that azure services are scalable, especially functions and especially functions on consumption plans... Edit, oh your comment has disappeared – TheGeneral Nov 11 '21 at 08:10
  • 1
    @TheGeneral I did a more digging into the logs after reading your comment, and I can see, that the process of uploading/deleting files is executed **two** times instead of one. Thanks for guiding me in the right direction! You are welcome to provide an official answer to this post. – mnc Nov 11 '21 at 08:13
  • 1
    :) no problems, good luck – TheGeneral Nov 11 '21 at 08:13
  • @TheGeneral - Please post your comments as an answer. Thanks. – Gaurav Mantri Nov 11 '21 at 11:04
  • @GauravMantri It is all good, I will post it as an answer myself otherwise, just giving him/her a chance to do it before, since they were the one who should be credited for an answer. I am keeping an eye on the post. – mnc Nov 11 '21 at 11:57

1 Answers1

0

For my particular case, the problem was that the StreamWriter code was executed twice, instead of the intended once. Thanks to user TheGeneral for guiding me in the right direction in the comments.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
mnc
  • 388
  • 1
  • 2
  • 11