Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c# - Get root directory of Azure Function App v2

I build an Azure Function App (v2). Configuration tasks necessary for all functions are done in a Setup class that is structured like the following:

[assembly: WebJobsStartup(typeof(Startup))]

internal class Startup : IWebJobsStartup
{
  public void Configure(IWebJobsBuilder builder)
  {
    Configuration = new ConfigurationBuilder()
                      .SetBasePath(<functionAppDirectory>)
                      .AddJsonFile("local.settings.json")
                      .Build();
    builder.AddDependencyInjection(ConfigureServices);  
  }

  public IConfiguration Configuration { get; set; }

  private void ConfigureServices(IServiceCollection services)
  {
    var connection = Configuration.GetConnectionString("<myconnection-string>");
    ...
  }
}

In ConfigureServices I want to read a connection string from a configuration file. For that the function app base folder has be specified with SetBasePath. But I found no way to get access to this path. According to https://github.com/Azure/azure-functions-host/wiki/Retrieving-information-about-the-currently-running-function an ExecutionContext can be injected in a function, which contains the path need. But how do I access ExecutionContext in my Startup class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this piece of code in your startup file. I have just tested it today for my project and it works on both cloud and local.

var executioncontextoptions = builder.Services.BuildServiceProvider()
    .GetService<IOptions<ExecutionContextOptions>>().Value;
var currentDirectory = executioncontextoptions.AppDirectory;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...