如何检查Azure功能是否在本地环境上运行? `RoleEnvironment`在Azure Functions中不起作用 [英] How to check Azure function is running on local environment? `RoleEnvironment` is not working in Azure Functions

查看:67
本文介绍了如何检查Azure功能是否在本地环境上运行? `RoleEnvironment`在Azure Functions中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中有一个条件,需要检查当前环境是否不是本地环境.我曾经使用过!RoleEnvironment.IsEmulated,现在它在Azure函数中不起作用,但在Cloud Service中起作用.云服务中也共享相同的代码,因此解决方案应与云服务和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中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆