System.IO.IOException: "Unable to remove the file to be replaced" using File.Replace in Azure App Service

111 Views Asked by At

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?

2

There are 2 best solutions below

2
On

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.

  • Here Below is my repro, I've created .net MVC application added a controller.

FileController:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;

namespace WebApplication1.Controllers
{
    public class FileController : Controller
    {
        private readonly IWebHostEnvironment _hostingEnvironment;

        public FileController(IWebHostEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult ReplaceFile()
        {
            // Get the physical path of the App_Data folder
            string appDataFolderPath = Path.Combine(_hostingEnvironment.ContentRootPath, "App_Data");

            // Specify the file names
            string sourceFileName = "source.pdf";
            string destinationFileName = "destination.pdf";

            // Build the full file paths
            string sourceFilePath = Path.Combine(appDataFolderPath, sourceFileName);
            string destinationFilePath = Path.Combine(appDataFolderPath, destinationFileName);

            try
            {
                if (System.IO.File.Exists(sourceFilePath))
                {
                    System.IO.File.Replace(sourceFilePath, destinationFilePath, null, false);
                    ViewBag.Message = "File replaced successfully!";
                }
                else
                {
                    ViewBag.Message = "Source file does not exist.";
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = $"Error: {ex.Message}";
            }

            return View("Index");
        }
    }
}

Application Folder Structure:

enter image description here

enter image description here

enter image description here

  • Pdf files which you want to replace must be in the App_Data folder, If not it shows Source file does not exist. enter image description here

Then I deployed it to my App service in azure portal. I can see my application deployed successfully and clicked on browse.

enter image description here

Iam able to see my source.pdf file replacing with destination.pdf.

0
On

The answer I got from Microsoft support was that they need to install a hotfix to the app service to fix this issue.