2

How bad it is to use Environment.SetEnvironmentVariable in Azure Functions? Based on my initial observation it seems it is working as expected (ie. storing the value and returning when I asked using Environment.GetEnvironmentVariable call) but not showing anywhere in the environment variables list when I open the function in Kudu explorer. Where it is setting, is it okay to use or any adverse effects of using it?

In my case, I have to get the user details using the token passed to Function headers. I don't want to keep retrieving the ID for the user again and again rather I want to cache some where. Thought of using Redis cache but a) I'm using V1 functions due to some libraries issue b)Just for one variable at least for now, I thought it is too costly. any other suggestions?

Thank you -Srikanth

Srikanth Alluri
  • 133
  • 1
  • 12
  • 1
    This is about the worst possible way to use an environment variable. Why not rather create a singleton-scoped class that holds that token and inject it to set the token once, then inject it wherever you need it? – Ian Kemp Oct 24 '20 at 23:02
  • thank @IanKemp. I thought it is bad but don't know why :), For the token anyways i'm doing that. but it is another variable that I have to get based on the AD User that authenticated. So, I got the token -> gives me the principal user id -> get the user record from CRM using this user id/email -> store that user record across invocations. Coz' I know this user is going to hit the function multiple times (it may be in 5 mins or more than that).This AD account will always have the same user record in CRM, it doesnt makes sense to me to keep retrieving the same record. – Srikanth Alluri Oct 24 '20 at 23:13
  • actually, I'm interested in learning why, where it stores, what complications it would produce etc. – Srikanth Alluri Oct 24 '20 at 23:14

1 Answers1

2

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.

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • Excellent, Thanks for the inputs @Hury Shen . This is what I'm looking for. As long as there are no major issues (loosing them when I restart function app may not be a big concern, possibly for that 1 call, it might take a bit of time to get the details but all the followed 100 calls will save time. – Srikanth Alluri Oct 26 '20 at 23:04