How can I test Azure on-premises? `RoleEnvironment` does not work in Azure Functions

I have a condition in the code where I need to check if the current environment is not local.i used !RoleEnvironment.IsEmulated

, now this doesn't work in Azure functions, but works in cloud service. The same code is also shared with the cloud service, so the solution must work with cloud services and azure features.

How can I check the current environment is not locally hosted / deployed?

+8


source to share


3 answers


You can use an approach similar to what the current runtime is using to determine if it works 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 an application setting named WEBSITE_INSTANCE_ID

+3


source


Building on Fabio Cavalcante's answer, here is a working Azure function that checks the current running environment (on-premises 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);
        }
    }
}

      

+3


source


You can use an AZURE_FUNCTIONS_ENVIRONMENT

environment AZURE_FUNCTIONS_ENVIRONMENT

that is installed automatically:

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

      

https://github.com/Azure/azure-functions-host/issues/4491

0


source







All Articles