Reading settings from a Azure Function

感情迁移 提交于 2020-11-30 06:20:10

问题


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

What am I doing wrong? Thanks


回答1:


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.




回答2:


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.




回答3:


You need to go to Platform Features -> Application settings and add it there.

Add the setting under App settings.


Reading the setting can be done by first adding this at the top:

using System.Configuration;

And then reading a setting with:

string setting = ConfigurationManager.AppSettings["url"];

Where url is your setting key. The setting variable will contain your setting value.



来源:https://stackoverflow.com/questions/43556311/reading-settings-from-a-azure-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!