如何在第一次请求之前确保 API 是 "warmed up"?

How to ensure API is "warmed up" before first request?

我有一个 xamarin android 应用程序,它向 .net 核心上托管的 api 发出请求(在 Windows 服务器上的 IIS 上)。初始请求总是需要很长时间才能加载(可能是因为一些预热过程)。如何确保 API 在用户需要发出请求时准备就绪?

我是否只在应用程序启动时发出 rapid 异步 get/post 请求?这似乎效率低下...

您需要为您的 API 使用健康检查:

public class ExampleHealthCheck : IHealthCheck
{
    public ExampleHealthCheck()
    {
        // Use dependency injection (DI) to supply any required services to the
        // "warmed up" check.
    }

    public Task<HealthCheckResult> CheckHealthAsync(
    HealthCheckContext context, 
         CancellationToken cancellationToken = default(CancellationToken))
    {
        // Execute "warmed up" check logic here.

        var healthCheckResultHealthy = true;

        if (healthCheckResultHealthy)
        {
            return Task.FromResult(
            HealthCheckResult.Healthy("The check indicates a healthy result."));
        }

        return Task.FromResult(
        HealthCheckResult.Unhealthy("The check indicates an unhealthy result."));
    }
}

将您的服务添加到健康检查服务:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks()
        .AddCheck<ExampleHealthCheck>("example_health_check");
}

在 Startup.Configure 中,在端点 URL:

的处理管道中调用 UseHealthChecks
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseHealthChecks("/health");
}

Link 到文档:https://docs.microsoft.com/ru-ru/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-2.2