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
71 views
in Technique[技术] by (71.8m points)

c# - Is it possible to persist an environment setting from within an Azure function?

I've created an Azure Function that retrieves new form inputs from a website, processes them and stores the result in another system by using an API call. I only want to retrieve the form inputs that have not been processed before. This is supported by the website. I'm reading the timestamp of the most recent form input that has already been processed. This works fine.

I'm using the following function to read the setting from the Azure function environment:

private static string GetEnvironmentVariable(string name)
{
    return System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}

After I've processed a form input, I store the timestamp of the form with the following function:

private static void SetEnvironmentVariable(string name, string value)
{
    System.Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.Process);
}

Everything seems to be working fine. I see in the logs that form inputs don't get processed more than once. However, when I take a look at the environment variables in the Azure dashboard, I can see that the initial value of the variable is still present. This initial value will be used when the environment 'shuts down' and is restarted (e.g. after changing the value of another environment variable).

I've tried to change the target from 'Process' to 'Machine', but this results in access control errors. There are some questions on SO that are related to my issue, but none of them provides me with an answer for my situation.

I would like to know whether:

  1. Environment variables are the / a suited solution for my use case;
  2. If so, how can I prevent that a variable will be reset to its initial value after resetting the Azure environment.

Thanks in advance!

question from:https://stackoverflow.com/questions/65875773/is-it-possible-to-persist-an-environment-setting-from-within-an-azure-function

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

1 Reply

0 votes
by (71.8m points)

Firstly, the Environment.SetEnvironmentVariable method already worked in your case.

Here is an answer from Hury Shen:

When you set the variable by Environment.SetEnvironmentVariable, it will not show in application setting. But we can use it by Environment.GetEnvironmentVariable as expected. Although the solution you mentioned is not so good, but it can implement your requirement. The adverse effect is when you restart the function app, the variables will be lost.

About the target Machine: The environment variable is stored or retrieved from the HKEY_LOCAL_MACHINESystemCurrentControlSetControlSession ManagerEnvironment key in the Windows operating system registry. This value should be used on .NET implementations running on Windows systems only.


One way to achieve but not set inside code:

In App Service, you can set app settings outside of your app code. Then you can access them in any class using the standard ASP.NET Core dependency injection pattern:

using Microsoft.Extensions.Configuration;

namespace SomeNamespace 
{
    public class SomeClass
    {
        private IConfiguration _configuration;
    
        public SomeClass(IConfiguration configuration)
        {
            _configuration = configuration;
        }
    
        public SomeMethod()
        {
            // retrieve nested App Service app setting
            var myHierarchicalConfig = _configuration["My:Hierarchical:Config:Data"];
            // retrieve App Service connection string
            var myConnString = _configuration.GetConnectionString("MyDbConnection");
        }
    }
}

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

...