1

I need to have access to FunctionAppDirectory in Azure Function

Here is a simplified version of the function

[Function("Test")]
public static HttpResponseData Test([HttpTrigger(AuthorizationLevel.Function, "post", Route = "Test")] HttpRequestData req, 
ExecutionContext context, FunctionContext fContext)
{
    var log = fContext.GetLogger(nameof(TestOperations));
    log.LogInformation(context?.FunctionAppDirectory?.ToString());
    return req.CreateResponse(HttpStatusCode.OK);
}

ExecutionContext here is null.

My Program.cs file

class Program
{
    static Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration(configurationBuilder =>
            {
                configurationBuilder.AddCommandLine(args);
            })
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(services =>
            {
                // Add Logging
                services.AddLogging();
            })
            .Build();

        return host.RunAsync();
    }
}

Azure Function running in .NET 5

How I can configure binding for ExecutionContext or get FunctionAppDirectory in other ways?

Mohamad Mousheimish
  • 1,641
  • 3
  • 16
  • 48
  • In .NET 5 (isolated), you can no longer use the Execution context - see https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide#execution-context. Do these things work for you? https://stackoverflow.com/questions/55616798/executioncontext-in-azure-function-iwebjobsstartup-implementation/55618131#55618131 – Alex AIT Jun 22 '21 at 11:43

1 Answers1

3

As Alex mentioned in the comment, azure function .net 5 is not support 'context.FunctionAppDirectory' to get the directory of function app now.

In function app 3.0, the 'ExecutionContext' is be designed in the package 'Microsoft.Azure.WebJobs'. But in your code, the ExecutionContext is from 'System.Threading'. So these are different classes.

You can use below code to get the azure function directory in .NET 5.0 azure function:

using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp6
{
    public static class Function1
    {
        [Function("Function1")]
        public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
            FunctionContext executionContext)
        {
            var logger = executionContext.GetLogger("Function1");
            logger.LogInformation("C# HTTP trigger function processed a request.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

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

            response.WriteString(actual_root);

            return response;
        }
    }
}
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27