I have a condition in code where i need to check if current environment is not local.i have used !RoleEnvironment.IsEmulated
, now this is not working in Azure functions but works in Cloud service. Same code is Shared in Cloud service also, so solution should work with cloud service and azure functions.
how can i check current environment is local not hosted/deployed?
Navigate to your function app in the Azure portal. Select Diagnose and solve problems to open Azure Functions diagnostics. Choose a category that best describes the issue of your function app by using the keywords in the homepage tile. You can also type a keyword that best describes your issue in the search bar.
Input Ctrl+C and Y to terminate functions.
Generally, for Azure Cloud Services you can use RoleEnvironment. IsAvailable to check if code is running in Web/Worker Role.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With