5
votes

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.

2

2 Answers

5
votes

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;
 }
0
votes

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.