How to check Azure function is running on local environment? `RoleEnvironment` is not working in Azure Functions(如何检查 Azure 功能是否在本地环境中运行?`RoleEnvironment` 在 Azure Functions 中不起作用)
问题描述
我在代码中有一个条件,我需要检查当前环境是否不是本地环境.我使用了 !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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查 Azure 功能是否在本地环境中运行?`RoleEnvironment` 在 Azure Functions 中不起作用


基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01