skip to Main Content

I need to get the directory of the folder for when the Azure Function is published. That directory will look something like homesitewwwroot. I have learned a good practice to get the folder is by using the ExecutionContext.FunctionDirectory to recieve the directory. So I used this in the code like following:

                    var context = new ExecutionContext();
                    _applicationDirectory = context.FunctionAppDirectory;

                    var config = new ConfigurationBuilder()
                        .SetBasePath(_applicationDirectory)
                        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                        .Build();

I tried to publish and test this, but it returns null.

Does anyone have any suggestions on how to recieve my directory so I can set the base path to the currenct directory.

I have so far tried the code I provided just above. I have read a few StackOverFlows suggestion aswell, but without luck. Some suggest to use the FunctionAppDirectory instead, but this resulted in the same issue.

Regarding debugging on Azure Portal, neither the Logs or Live Metrics provides anything else than the null exception.

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution to the problem. The way I first thought was the correct possible way, is not the way to do it. By using the following code, I am able to recive the folder Chomewwwroot:

                var fileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
                string path = fileInfo.Directory.Parent.FullName;
    

  2. I tried in my environment and got the below results:

    I tried the below C# with logging method to get my Function Directory:-

    using System.IO;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Logging;
    
    namespace MyFunctionApp
    {
        public static class MyHttpTrigger
        {
            [FunctionName("MyHttpTrigger")]
            public static IActionResult Run(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
                ILogger log,
                ExecutionContext context)
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                var functionDirectory = context.FunctionAppDirectory;
                log.LogInformation($"Function directory: {functionDirectory}");
    
                return new OkObjectResult($"Function directory: {functionDirectory}");
            }
        }
    }
    

    Local Output:-

    enter image description here

    Then I published this function to my FunctionApp in Azure portal and Tested the function, I got the results below:-

    Portal Output:-

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search