'Dictionary values shared across executions when deployed to Azure Function App

I am new to Azure functions.

What I am trying to do: I am taking an input of Tags and the resourceId and I am updating the tags for that resource.

Below is my c# class.

 public static Dictionary<string, string> newTagsToBeApplied = new Dictionary<string, string>();

 public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest request,
            ILogger log, ExecutionContext context)

{
 var tagsData = JsonConvert.SerializeObject(request.tags);

 //Creating tags dictionary for updating the tag values

JObject TagsJObject = JObject.Parse(tagsData);
newTagsToBeApplied = TagsJObject.ToObject<Dictionary<string, string>>();

.
.
.

var tagResponse = await AddorUpdateTagsAtScope(credentials, httpClient, newTagsToBeApplied, scope, subscriptionTagEndpoint);

}

What is Happening? It works fine in local. But when it deployed to functionapp, it is working fine for first run and from second run, it is taking cached values(tag values supplied in the previous request) rather than the supplied input values.

Below is my UpdateTags method:

public async Task<bool> AddorUpdateTagsAtScope(ServiceClientCredentials client, HttpClient httpClient,
            Dictionary<string, string> tags, string scope, string subscriptionTagEndpoint)
        {
            var cancellationToken = new CancellationToken();

            //Remove null entires
            tags = (from kv in tags
                    where kv.Value != null
                    select kv).ToDictionary(kv => kv.Key, kv => kv.Value);

            var json = JsonConvert.SerializeObject(new
            {
                properties = new
                {
                    tags = tags
                },
                operation = "merge"
            }
            );

            var requestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Patch,
                RequestUri = new Uri(subscriptionTagEndpoint),
                Content = new StringContent(json, Encoding.UTF8, "application/json")
            };

            client.ProcessHttpRequestAsync(requestMessage, cancellationToken).GetAwaiter().GetResult();

            var response = await httpClient
                .SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

            if (response.IsSuccessStatusCode)
            {               
                return true;
            }
            else
            {             
                throw new Exception(await response.Content.ReadAsStringAsync());
            }
        }

Question:

Is there a way where I can get the actual passed values instead of cached values?



Solution 1:[1]

Avoid use static variables for data. In Azure Functions, static varialbes may or may not be kept between function invocations.

Run doesn't need to be static:

public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
    ILogger log)

Instead of keeping tags as a field pass the tags to the next method:

public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
    ILogger log)
{
  var tags = ...extract tags...
  ...
  ProcessMoreAsync(tags);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tymtam