none
Get Executing Directory When Published RRS feed

  • Question

  • Hello, this seems trivial, but I can't figure it out.

    I have a C# Azure Functions TimerTrigger project that contains some files that are published on the file system.

    When running locally on my development computer, I can execute this:

    File.OpenText(string.Format("./EmailTemplates/{0}", TemplateName))

    And this will successfully retrieve a file I have in my project's root folder where it's executing.

    When I publish this to Azure, it tries to open D:\Windows\system32\EmailTemplates\abc.txt

    The application however is running in: D:\home\site\wwwroot.  What is being returned I'm suspecting that's where the Azure Functions process is running.

                log.Info($"Working Directory: {System.Environment.CurrentDirectory}");
                log.Info($"Current Directory: {System.IO.Directory.GetCurrentDirectory()}");

    These will return:

    Working Directory: D:\Windows\system32
    Current Directory: D:\Windows\system32

    Does anyone know how to get the currently executing assembly's directory when deployed to Azure?

    Thanks

    Friday, November 10, 2017 5:58 PM

Answers

  • Just add the 'ExecutionContext context' param to your function signature and it will flow through.

    David

    • Marked as answer by Ryan_Ha Friday, November 10, 2017 9:37 PM
    Friday, November 10, 2017 9:21 PM

All replies

  • Hi Ryan,

    Try using ExecutionContext.FunctionAppDirectory. See sample here.

    thanks,
    David

    • Proposed as answer by David Ebbo Friday, November 10, 2017 9:21 PM
    Friday, November 10, 2017 8:10 PM
  • Thanks, unfortunately in a TimerTrigger Function, we don't have the context variable:

      public static async void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, TraceWriter log)

    Is there another way that you know how to get to it?

    Thanks.

    Friday, November 10, 2017 9:07 PM
  • Just add the 'ExecutionContext context' param to your function signature and it will flow through.

    David

    • Marked as answer by Ryan_Ha Friday, November 10, 2017 9:37 PM
    Friday, November 10, 2017 9:21 PM
  • AWESOME! Thank you, that worked perfectly!

    Friday, November 10, 2017 9:37 PM
  • What is the equivalent technique for Java functions? The ExecutionContext doesn't appear to have a similar method.

    Thanks

    Tuesday, November 21, 2017 12:17 AM
  • @Jack: please start a new thread to make sure this gets proper attention, as Java works a bit differently. Thanks!
    Tuesday, November 21, 2017 12:46 AM
  • Hi 

    I have tried creating an azure function V2 using ASP.net core and EF core.There I am using the Starp.cs file and inside that I am doing all my Startup activity like , setting the DBContext , Setting all the dependency .
    But inside this I am not able to get the context ("Execution Context") .It always returns me "null" value . The execution context i wanted to use to set the BasePath of the configuration. But the same is always null in the Startp.cs. 

                var configuration = new ConfigurationBuilder()
                                .SetBasePath(context.FunctionDirectory)
                                .AddJsonFile("local.settings.json", true, reloadOnChange: true)
                                .AddEnvironmentVariables()
                                .Build();

    So please let me know how can make the above code to work.

    Appreciate your Support .

    Regards,
    Kutti
    Thursday, February 28, 2019 7:08 AM