Search code examples
azureasp.net-coreazure-web-app-servicehangfire

"Access to the path 'C:\\home\\site\\wwwroot\\dataModel.csv' is denied." in a .NET Core app, deployed in Azure


I have an ASP.NET Core API app which runs some background processes via HangFire. One of the process includes writing a csv file onto the wwwroot folder as following:

public async Task Work(PerformContext context)
        {
            var latestLikes = await this.likeRepository
                .All()
                .Select(l => new LatestLikesServiceModel
                {
                    UserId = l.UserId,
                    BeatId = l.BeatId,
                })
                .ToListAsync();

            var modelPath = this.webHostEnvironment.ContentRootPath + "\\dataModel.csv";
            using (var writer = new StreamWriter(modelPath))
            using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                csv.WriteRecords(latestLikes);
            }
        }

In localhost it works perfectly, but when I deploy it in azure the HangFire log returns: "System.UnauthorizedAccessException","ExceptionMessage":"Access to the path 'C:\home\site\wwwroot\dataModel.csv' is denied."
How can I resolve this issue?


Solution

  • After one week of researching I finally found out the solution
    I should have mentioned that I use Azure DevOps for CI & CD.
    By default the azure app service is deployed as a zip (which cuts the direct access to the file system) What I had to do was to change the Deployment method to a Web Deploy (Additional Deployment Options) and I finally have access to the file system. For more descriptive information, please refer:
    https://tomasherceg.com/blog/post/azure-app-service-cannot-create-directories-and-write-to-filesystem-when-deployed-using-azure-devops#comments