How to get Function APP Root directory in .NET 5 isolated process

Dum, Rupak 21 Reputation points
2022-05-05T09:40:51.463+00:00

I am developing HTTPTrigger using Azure Function APP in .NET 5 isolated process.

Previously while developing Azure Function APP in .NET Core 3.1 we used to get function APP root directory using "executionContext.FunctionAppDirectory"

But I find we can no longer use the "executionContext.FunctionAppDirectory" to get the function APP root directory .

I need function APP root directory to map a custom json file stored in a folder in the Azure function APP

using FileStream openStream = File.OpenRead("C:\thepaymentgroup\projects\tepi\Tyler.Energov.AF\json\HPPConfiguration.json");

Need to get the function APP root directory to replace the hard coded part.

I have tried this code to get the function APP root directory

var local_root = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
var azure_root = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
var actual_root = local_root ?? azure_root;

But the problem with the above code is I am getting :
local_root : C:\thepaymentgroup\projects\tepi\Tyler.Energov.AF\bin\Debug\net5.0

But I need upto C:\thepaymentgroup\projects\tepi\Tyler.Energov.AF

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

Accepted answer
  1. MughundhanRaveendran-MSFT 12,506 Reputation points
    2022-05-10T17:10:21.58+00:00

    Hi @Dum, Rupak ,

    Thanks for reaching out to Q&A forum.

    I understand that you are trying to read a custom json file from the root directory when the Function app is deployed to the Azure Functions runtime. For customers to store files, we always recommend to be stored in the "D:\home\site\wwwroot" directory. The drive may vary, it may be either C or D. The deployed files and dlls would also go to this location which you would have observed.

    So I would suggest you to place the file in "D:\home\site\wwwroot" directory and read dynamically using the below code

    var azure_root=Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot";

    The reason why we suggest not to use other locations is that, the fileshare that is mounted with the backend VM that hosts the function app will change periodically and the contents might be lost. However the contents in the "D:\home\site\wwwroot" directory will always remain and wont be lost.

    I hope this helps! Feel free to reach out to me if you have any queries or concerns.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.