问题
What is the best way to test API endpoints within azure? I am looking to get alerted if an endpoint is not working.
回答1:
Take a look at webtest feature in Application Insights.
https://docs.microsoft.com/en-us/azure/application-insights/app-insights-monitor-web-app-availability
回答2:
I suggest you create a WebJob to test your API endpoints. In your WebJob, you could use a TimerTrigger to run the test function timely(For example, every 2 minutes).
To use TimerTrigger, you need to install Microsoft.Azure.WebJobs.Extensions package using NuGet. After that, you could configure the WebJob to use the timer extension using following code.
static void Main()
{
var config = new JobHostConfiguration();
//Configure WebJob to use TimerTrigger
config.UseTimers();
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
In the function, you could send a request to your Web API. If you can't get the response from the server or the response status is not equal to 200 OK, it means that the Web API is not useable.
public static void StartupJob([TimerTrigger("0 */2 * * * *", RunOnStartup = true)] TimerInfo timerInfo)
{
WebRequest request = WebRequest.Create("URL of your api");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)
{
//API is not useable
}
}
回答3:
you can write a custom Azure Function to report Telemetry to Application Insight. See: https://github.com/rbickel/Azure.Function.AppInsightAvailabilityTest
来源:https://stackoverflow.com/questions/43720824/how-can-you-create-an-api-health-check-in-azure