6

I am new to azure. I have an Asp.Net MVC app that is hosted on azure(app service) with two instances, my app uses local cache. Sometimes i need to clear this cache. But the problem is that when i clear cache i actually do this only for one particular instance, and other one still keeps old cache. Is there any way to get access for all instances on app service?

Ofcourse i can run some background task on each instance to achive this, but this does not seems convenient. I would like to have ability to get all IPs and ports of all instances on app service, then create webjob that would hit all instances and clears cache.

2 Answers2

7

It is possible to do by using ARRAffinity cookie in request. Code sample:

private static async Task<HttpResponseMessage> GetFromInstance(Uri url, string instanceId)
    {
        var cookieContainer = new CookieContainer();
        using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
        {
            using (var httpClient = new HttpClient(handler))
            {
                cookieContainer.Add(url, new Cookie("ARRAffinity", instanceId));
                return await httpClient.GetAsync(url);
            }
        }
    }

One option to get list of instanses ids is via Azure Resource Explorer https://resources.azure.com

5

There is no direct way to connect to a specific instance of a web app. You'd need to either build some type of messaging system to trigger all your app instances to clear their local caches, or shift to a shared cache (which is more in line with hosting in App Services).

David Makogon
  • 69,407
  • 21
  • 141
  • 189