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

c# - can you store a value in a global dictionary in a Azure Function

I have created an azure function with 2 HTTP triggers, now the issue I am facing is I need to store a value from the first HTTP trigger inside a dictionary like the one below

static Dictionary<int, TargetRequest> Target = new Dictionary<int, TargetRequest>();

This is inside my function class but outside the trigger. Now after another HTTP trigger I need to retrieve that value from the dictionary and use it, but only after it's received a trigger. Being completely new to Azure I'm not sure if the values in that dictionary stay or not as it seems to work on localhost but not when deployed to Azure. If they don't, what is another way to go about this. Is using an Azure Queue Storage the way to store this value or is there another way to go about this?

Ps: TargetRequest is a c# class, if that makes any difference.

Cheers

question from:https://stackoverflow.com/questions/66055574/can-you-store-a-value-in-a-global-dictionary-in-a-azure-function

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

1 Reply

0 votes
by (71.8m points)

Unless you're using Durable Functions that Perkins mentioned (which come with it's own complexity and constraints that you need to understand), you need to imagine each instance of your Function running on a new different laptop every time it runs. To underline, one run = a new/different laptop. You can not ever rely on "instance reuse", i.e. same laptop being used twice in two different runs. This is of course purely from design point of view.

Two options:

  1. Using Durable Functions
  2. Build your own cache using appropriate service. Storage Q is not an appropriate choice. Better examples are Redis, any transactional DB, ADLS, etc. See some architectural guidance that Azure provides around caching. This is a common problem in SaaS/Cloud world.

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

...