We have code deployed to an Azure App Service that uses System.IO.File.Replace() to replace a file in the temporary folder of the web app:
File.Replace("c:\local\temp\source.pdf", "c:\local\temp\destination.pdf", "c:\local\temp\original.pdf", false)
This was working for several years until a couple weeks ago. It started throwing an Exception:
System.IO.IOException: Unable to remove the file to be replaced. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalReplace(String sourceFileName, String destinationFileName, String destinationBackupFileName, Boolean ignoreMetadataErrors)
I tried connecting in kudu console to the web app and running the same command in powershell:
[System.IO.File]::Replace("c:\local\temp\source.pdf", "c:\local\temp\destination.pdf", "c:\local\temp\original.pdf", $false)
and I got back the same error message: "Unable to remove the file to be replaced."
I tried the same operation on a different app service on the same app service plan and got the same error, but on a different app service plan, the operation worked fine. I tried scaling the app service plan to different sizes but that didn't resolve it.
I opened a Microsoft support ticket but they have not been able to resolve yet.
I am wondering if anyone else has experienced the same issue or has a resolution?
The
File.Replace
method in C# is designed to atomically replace one file with another. However, it comes with certain conditions and limitations, and its behavior can be platform-specific.FileController:
Application Folder Structure:
Source file does not exist.
Then I deployed it to my App service in azure portal. I can see my application deployed successfully and clicked on browse.
Iam able to see my
source.pdf
file replacing withdestination.pdf
.