问题
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