How to get the hostname for where the azure function run from timer trigger?

六月ゝ 毕业季﹏ 提交于 2021-02-10 14:36:33

问题


I have a timertrigger

    [FunctionName("HeathChecker")]
    public static void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");


    }

And an HTTP trigger

    [FunctionName("AspNetCoreHost")]
    public Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, Route = "{*all}")]HttpRequest req,
        [AspNetCoreRunner(Startup = typeof(Startup))] IAspNetCoreRunner aspNetCoreRunner,
        ExecutionContext executionContext)
    {

        return aspNetCoreRunner.RunAsync(executionContext);
    }

I would like to be able to get the hostname of where the HTTP trigger is running, so I can call it with HTTP from the timer trigger. Is this possible?


回答1:


Yo could get host name with:

Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME")

You could check the value in your Function Kudu(https://sitename.scm.azurewebsites.net) page, select the menu "Environment", there are WEBSITE_HOSTNAME or SERVER_NAME.




回答2:


You can also get the host name from the HttpRequest req object:

HostString hostString = req.Host;
string host = hostString.Host;
int? port = hostString.Port;
string fullHost = hostString.Value; 



回答3:


I just checked this and found out that the environment variable name has changed. What's working now is this:

Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")


来源:https://stackoverflow.com/questions/56586433/how-to-get-the-hostname-for-where-the-azure-function-run-from-timer-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!