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 中不起作用


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01