c# - 如何检查Azure函数是否在本地环境中运行? `RoleEnvironment` 无法在 Azure Functions 中运行

标签 c# azure azure-functions azure-cloud-services

我在代码中有一个条件,我需要检查当前环境是否不是本地的。我已经使用了 !RoleEnvironment.IsEmulated,现在这在 Azure 函数中不起作用,但在云服务中起作用。相同的代码也在云服务中共享,因此解决方案应该与云服务和azure功能一起使用。

我如何检查当前环境是本地的而不是托管/部署的?

最佳答案

根据 Fabio Cavalcante 的回答,以下是一个可运行的 Azure 函数,用于检查当前运行环境(本地或托管):

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);
        }
    }
}

关于c# - 如何检查Azure函数是否在本地环境中运行? `RoleEnvironment` 无法在 Azure Functions 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45026215/

相关文章:

c# - 无效的 URI : The format of the URI could not be determined

c# - 列表订阅操作 : 403 forbidden

function - 如何检查Azure Function是否仍在运行

Azure Functions 执行错误

azure - 限制 Azure Function App 中的 Azure 存储队列处理

c# - 查看asp.net mvc和asp.net core mvc的目录编译时间差异

c# - 拨出电话录音 [UCMA, S4B]

c# - 使用 Asp.Net MVC 处理临时 session 数据

.net - Azure Web角色MVC 4安装问题

Azure Function 在应用程序服务计划中调用自身两次