How to check Azure function is running on local environment? `RoleEnvironment` is not working in Azure Functions How to check Azure function is running on local environment? `RoleEnvironment` is not working in Azure Functions azure azure

How to check Azure function is running on local environment? `RoleEnvironment` is not working in Azure Functions


Based on answer by Fabio Cavalcante, here is a working Azure function that checks the current running environment (local or hosted):

using System.Net;using System.Net.Http;using System.Threading.Tasks;using Microsoft.Azure.WebJobs;using Microsoft.Azure.WebJobs.Extensions.Http;using Microsoft.Azure.WebJobs.Host;using System;namespace AzureFunctionTests{    public static class WhereAmIRunning    {        [FunctionName("whereamirunning")]        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)        {            bool isLocal = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));            string response = isLocal ? "Function is running on local environment." : "Function is running on Azure.";            return req.CreateResponse(HttpStatusCode.OK, response);        }    }}


You can use the AZURE_FUNCTIONS_ENVIRONMENT environment variable, which is set automatically in local development:

Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"); // set to "Development" locally

Note that when deployed/published (i.e. Azure), you'd need to set the environment variable yourself (e.g. function app settings in Azure).


You can use an approach similar to what the actual runtime uses to identify whether it is running on Azure:https://github.com/Azure/azure-webjobs-sdk-script/blob/efb55da/src/WebJobs.Script/Config/ScriptSettingsManager.cs#L25

In this case, the runtime checks for the presence of an app setting named WEBSITE_INSTANCE_ID