代码之家  ›  专栏  ›  技术社区  ›  Sql Surfer

Azure函数“当前的Azure资源名称”

  •  0
  • Sql Surfer  · 技术社区  · 7 年前

    如果我在一个TimerTrigger的Azure函数中,如何在运行时获取已部署的Azure资源的名称。

    在httpTrigger中,您可以简单地执行以下操作: req.RequestUri.AbsoluteUri 然后得到一个好的 部署时 “运行时说明。

    在计时器中,它不像在timerfuncton执行时如何获取Azure资源名称或路径那么明显。

    我想要类似于microsoft.azure.webjobs.executioncontext的东西,它提供编译时函数信息,但我正在从正在执行TimerTrigger函数的正在运行的Azure资源中寻找一些运行时信息。

    我的回退是将我的Azure资源名硬编码到一个环境变量中,并在运行时读取它。看起来很奇怪,但会有用的。

    谢谢克劳迪乌

    以下是有关这方面的一些最终注释:

    // Environment Variable Usage 
    string res = System.Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME", EnvironmentVariableTarget.Process);
    //  WEBSITE_HOSTNAME is like "myAzureDeployedFuncName.azurewebsites.net" 
    //  WEBSITE_HOSTNAME is like "localhost:7072" 
    string res = System.Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME", EnvironmentVariableTarget.Process);
    //  WEBSITE_SITE_NAME is "myAzureDeployedFuncName" on Azure 
    //  WEBSITE_SITE_NAME is not there in local debug function runtime 
    
    // using AppSettings is not the same and usign the above GetEnvironmentVariable call 
    string res = System.Configuration.ConfigurationManager.AppSettings["WEBSITE_HOSTNAME"]; // not there for Settings interface
    //  WEBSITE_HOSTNAME will be blank - it is not part of "Settings"
    string res = System.Configuration.ConfigurationManager.AppSettings["WEBSITE_SITE_NAME"]; 
    //  WEBSITE_SITE_NAME is "myAzureDeployedFuncName" on Azure when using "settings" instead of Environment variable 
    //  WEBSITE_SITE_NAME is not there in local debug function runtime 
    // the above AppSettings are not the same as GetEnvironmentVariable behavior at runtime 
    

    从一个函数应用程序计时器触发器调用“你自己”,这是有用的…

    website_hostname=myazuredeployedfucname.azurewebsites.net

    Some Other potentially useful Env Vars:
    // next one has scm in name
    HTTP_HOST = myAzureDeployedFuncName.scm.azurewebsites.net
    // slot help
    APPSETTING_WEBSITE_SLOT_NAME = Production
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Claudiu Guiman    7 年前

    您可以从“website\u site\u name”环境变量中读取。

    如果您转到kudu,可以找到更多有用的变量:“https://.scm.azurewebsites.net/env.cshtml”

    推荐文章