Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading settings from a Azure Function

I'm new to Azure's function... I've created a new timer function (will be fired every 30 minutes) and it has to perform a query on a URL, then push data on the buffer..

I've done

public static void Run(TimerInfo myTimer, TraceWriter log)
{
 var s = CloudConfigurationManager.GetSetting("url");
 log.Info(s);
}

And in my function settings I've

enter image description here

What am I doing wrong? Thanks

like image 452
advapi Avatar asked Apr 22 '17 07:04

advapi


People also ask

What is AzureWebJobsStorage used for?

AzureWebJobsStorage. The Azure Functions runtime uses this storage account connection string for normal operation. Some uses of this storage account include key management, timer trigger management, and Event Hubs checkpoints. The storage account must be a general-purpose one that supports blobs, queues, and tables.

How do I get Azure function connection string?

Get connection informationSign in to the Azure portal. Select SQL Databases from the left-hand menu, and select your database on the SQL databases page. Select Connection strings under Settings and copy the complete ADO.NET connection string.


2 Answers

Note that for Azure Functions v2 this is no longer true. The following is from Jon Gallant's blog:

For Azure Functions v2, the ConfigurationManager is not supported and you must use the ASP.NET Core Configuration system:

  1. Include the following using statement:

    using Microsoft.Extensions.Configuration;
    
  2. Include the ExecutionContext as a parameter

    public static void Run(InboundMessage inboundMessage, 
        TraceWriter log,
        out string outboundMessage, 
        ExecutionContext context)
    
  3. Get the IConfiguration Root

    var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
    
  4. And use it to reference AppSettings keys

    var password = config["password"]
    

When debugging locally, it gets the settings from local.settings.json under the "Values" keyword. When running in Azure, it gets the settings from the Application settings tab.

like image 97
Pitchmatt Avatar answered Oct 26 '22 06:10

Pitchmatt


You can use System.Environment.GetEnvironmentVariable like this:

var value = Environment.GetEnvironmentVariable("your_key_here")

This gets settings whenever you're working locally or on Azure.

like image 37
DSpirit Avatar answered Oct 26 '22 06:10

DSpirit