Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutionContext in Azure Function IWebJobsStartup implementation

How to get access to the ExecutionContext.FunctionAppDirectory in Functions Startup class so I can setup my Configuration correct. Please see the following Startup code:

[assembly: WebJobsStartup(typeof(FuncStartup))]
namespace Function.Test
{
    public class FuncStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            var config = new ConfigurationBuilder()
               .SetBasePath(“”/* How to get the Context here. I cann’t DI it 
                           as it requires default constructor*/)
               .AddJsonFile(“local.settings.json”, true, reloadOnChange: true)
               .AddEnvironmentVariables()
               .Build();

        }
    }
 }
like image 932
Soma Yarlagadda Avatar asked Apr 10 '19 15:04

Soma Yarlagadda


People also ask

What is ExecutionContext in Azure function?

ExecutionContext. The execution context enables interaction with the Azure Functions execution environment. HttpRequestMessage<T> An HttpRequestMessage instance is provided to Azure functions that use HttpTrigger.

How to implement iwebjobsstartup using Azure Functions?

We've done it by following a few steps: 1 Create the Startup class and implement the IWebJobsStartup interface 2 Add an attribute that specifies which class must be used as the Startup class for this assembly 3 Transform the Azure function to make it non-static 4 Inject the dependency in the constructor. More ...

Why don't I have executioncontext in my Azure Functions?

You don't have the ExecutionContext since your Azure Function is not yet processing an actual function call. But you don't need it either - the local.settings.json is automatically parsed into the environment variables.

How do I bind to an executioncontext in webjobs?

WebJobs lets you bind to an ExecutionContext. With this binding, you can access the ExecutionContext as a parameter in your function signature. For example, the following code uses the context object to access the invocation ID, which you can use to correlate all logs produced by a given function invocation.

How do I use the files binding in azure webjobs?

To use the Files binding, install Microsoft.Azure.WebJobs.Extensions and call UseFiles. WebJobs lets you bind to an ExecutionContext. With this binding, you can access the ExecutionContext as a parameter in your function signature.


1 Answers

You don't have the ExecutionContext since your Azure Function is not yet processing an actual function call. But you don't need it either - the local.settings.json is automatically parsed into the environment variables.

If you really need the directory, you can use %HOME%/site/wwwroot in Azure, and AzureWebJobsScriptRoot when running locally. This is the equivalent of FunctionAppDirectory.

This is also a good discussion about this topic.

    public void Configure(IWebJobsBuilder builder)
    {
        var local_root = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
        var azure_root = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";

        var actual_root = local_root ?? azure_root;

        var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
            .SetBasePath(actual_root)
            .AddJsonFile("SomeOther.json")
            .AddEnvironmentVariables()
            .Build();

        var appInsightsSetting = config.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY");
        string val = appInsightsSetting.Value;
        var helloSetting = config.GetSection("hello");
        string val = helloSetting.Value;

        //...
    }

Example local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "APPINSIGHTS_INSTRUMENTATIONKEY": "123456..."
  }
}

Example SomeOther.json

{
  "hello":  "world"
}
like image 65
Alex AIT Avatar answered Sep 20 '22 21:09

Alex AIT