0

Currently, by default, Azure function base path is set to "D:\home\site\wwwroot". For example, when publishing, VS uploads app to this folder.

I need to read config file from this folder. We have problem of ExecutionContext is null via dependency injection via constructor

Setting a new environment variable might cause issue if the path is changed in the future.

My question is that how can I use app base path that is reliable and stable, that works with DI via constructor.

Azure Function 2.x

VS 2017

Pingpong
  • 7,681
  • 21
  • 83
  • 209

3 Answers3

1

you can use function.json to have your configuration key pairs. for example:

System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);

and in function.json you can do like this: "mykey": "myvalue"

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.24",
  "configurationSource": "attributes",
  "bindings": [
  {
      "direction":"in",
      "type": "timerTrigger",
      "useMonitor": true,
      "runOnStartup": false,
      "name": "myTimer",
      "mykey": "myvalue"
  }
  ],
      "disabled": false,
      "scriptFile": "../bin/**.dll",
      "entryPoint": "**.Run"
} 
knowdotnet
  • 839
  • 1
  • 15
  • 29
  • As I mentioned on OP: Setting a new environment variable might cause issue if the path is changed in the future. – Pingpong Mar 16 '19 at 19:49
0

There is an environment variable pointing to home directory. This would not change as many services including function app take dependency on it. Below is how function runtime fetches it in azure environment.

    string home = GetEnvironmentVariable("HOME");
    Path = System.IO.Path.Combine(home, "site", "wwwroot");
Naren
  • 847
  • 4
  • 7
  • Is it possible to provide links? – Pingpong Mar 19 '19 at 09:06
  • As mentioned in the other post. This is how Azure app service and Azure functions determines this path. It is stable and consistent across all the hosting environments. The wiki link on the Azure functions documents this partially here: https://github.com/Azure/azure-functions-host/wiki/Configuration-Settings – Naren Mar 19 '19 at 21:14
0

Hope this can help others with latest Azure Function App version (v4). I have ApplicationInsights and it logs TRACE for me the ContentRootPath

Content root path: /home/site/wwwroot

application_insights_log_traces.jpg

If you are using Azure Function isolated-process (worker). You are able to use HostBuilderContext to get base path easily. For example below:

var startup = new Startup();
var host = Host
            .CreateDefaultBuilder(args)
            .ConfigureFunctionsWorkerDefaults(builer => { ... })
            .ConfigureAppConfiguration((context, builder) =>
            {
                builder.AddJsonFile(
                   Path.Combine(context.HostingEnvironment.ContentRootPath, "appsettings.json"),
                   false,
                   true
                );
            })
            .ConfigureServices(startup.ConfigureServices)
            .Build();
host.Run();

In the your_azurefuncapp_project.csproj

<Project Sdk="Microsoft.NET.Sdk">
   <ItemGroup>
        <None Update="host.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>

        <!-- Should publish this file along to -->
        <None Update="appsettings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>
</Project>
Triet Nguyen
  • 763
  • 9
  • 20