如何检查 Azure 功能是否在本地环境中运行?`RoleEnvironment` 在 Azure Functions 中不起作用

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)