问题描述
我在代码中有一个条件,我需要检查当前环境是否不是本地环境.我使用了 !RoleEnvironment.IsEmulated
,现在这在 Azure 函数中不起作用,但在云服务中起作用.相同的代码也在云服务中共享,因此解决方案应与云服务和天蓝色功能一起使用.
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?
推荐答案
根据 Fabio Cavalcante 的回答,这是一个有效的 Azure 函数,用于检查当前运行环境(本地或托管):
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);
}
}
}
这篇关于如何检查 Azure 功能是否在本地环境中运行?`RoleEnvironment` 在 Azure Functions 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!