7

How can I get the name of the slot (production or staging) of my app service when the asp.net core process starts.

The HTTP_HOST environment variable doesn't appear to be set on startup and I have no http request to inspect.

Clement
  • 3,990
  • 4
  • 43
  • 44
  • Does this answer your question? [Staging or Production Instance?](https://stackoverflow.com/questions/4328462/staging-or-production-instance) – yoel halb May 11 '22 at 20:39

2 Answers2

7

If we want to get the host name, you could use the environment variable WEBSITE_HOSTNAME to do that.

var hostName = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");

If you run it on the slot environment then you will get the value youwebsiteName-slotName.azurewebsites.net. Or if you run it on the production environment you will get the value youwebsiteName.azurewebsites.net.

Update:

We could use the appsetting slot to do that.

1.Go to the slot webApp and config the appsetting and check slot setting.

enter image description here

2.Please use the following code to get the slot name.

 string name = string.Empty;
 var sitName = Environment.GetEnvironmentVariable("APPSETTING_WEBSITE_SITE_NAME", EnvironmentVariableTarget.Process);
 var slotName = Environment.GetEnvironmentVariable("APPSETTING_KeyName", EnvironmentVariableTarget.Process); //APPSETTING_Test_Slot
 if (!string.IsNullOrEmpty(slotName))
 {
     name = sitName + "-" + slotName;
 }
 else
 {
     name = sitName;
 }
Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • If you run it on the slot environment then you will get the value youwebsiteName-slotName.azurewebsites.net. Or if you run it on the production environment you will get the value youwebsiteName.azurewebsites.net. I have updated the answer and added the description of it. – Tom Sun - MSFT May 03 '18 at 20:58
  • Actually it always returns the same values on either slot – Clement May 03 '18 at 21:18
  • You could check WEBSITE_HOSTNAME with your slot kudu (environment) – Tom Sun - MSFT May 03 '18 at 21:23
  • 1
    Sorry, they are actually different but it doesn't change upon swapping. So it's pretty useless. See https://feedback.azure.com/forums/169385-web-apps/suggestions/19441249-website-hostname-environment-variable-is-not-corre – Clement May 03 '18 at 23:33
  • If I understand correctly, the swapping action is just change DNS redirect from production to slot, not the host changed between production and slot. – Tom Sun - MSFT May 04 '18 at 00:55
  • That is correct. But I need a way to ask programatically, as of now, which slot am I in? – Clement May 04 '18 at 04:31
  • We could use the slot setting to do that. I also update the answer. – Tom Sun - MSFT May 04 '18 at 05:21
  • This would work but the problem is that having any slot setting will cause azure to restart the app when swapping staging and prod, which is not cool since for us the main reason to use slots is to avoid an interuption of service. – Clement May 04 '18 at 23:49
  • Based on my knowledge, on the azure function app we could get slot name with enviroment variables [`APPSETTING_WEBSITE_SLOT_NAME`](https://stackoverflow.com/questions/46085155/how-to-know-if-an-azure-function-is-running-in-a-slot/46087043#46087043). But I can't find it with azure webApp, I recommend that you could give your [feedback](https://feedback.azure.com/forums/169385-web-apps) to Azure team. – Tom Sun - MSFT May 14 '18 at 02:02
  • Using the suggested solution (like in the screen capture) worked for me: slotName = Environment.GetEnvironmentVariable("test_KeyName", EnvironmentVariableTarget.Process); "test_KeyName" was defined as Staging or production and is slot specific – Meir Schreiber May 23 '18 at 09:28
  • Hello @Clement . I think I am in the same situation that you were ( that is described also here feedback.azure.com/forums/169385-web-apps/suggestions/…) Can you please tell me if you managed to solve it ? – Daniel May 31 '19 at 13:00
  • 1
    @Daniel we use Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") but it doesn't change when swapping (until the app is restarted) :( – Clement Jun 01 '19 at 00:04
0

I think what the poster wants to do, is have a background process run only in one of the deployment slots, and when in production. Even if this worked, one issue you might have is if the worker process is doing something and then checking if it should keep doing work based on the slot it's in, and you swap, you might have a point where both processes are running. You also run the risk if you have this kind of background process of killing them mid-cycle.

What I do, is have an endpoint that can spin up and spin down the process. So right before I swap, I spin down the one in prod, and after the swap, I spin it back up. This can simply be done with a static var on the worker process that you set to true or false, and your event loop checks it each time through, if it's false, it checks again in 2 minutes or whatever.

I then have a logic app that runs every 5 minutes, to set it to true, so I don't have to think about it every time I do a deploy... and if the app recycles, it fires back up again.

Jeremy
  • 21
  • 2