在第一次请求之前如何确保API“预热”?

问题描述 投票:-2回答:1

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

我是否只是在应用启动时进行快速异步获取/发布请求?这看起来效率不高......

.net-core xamarin.android kestrel
1个回答
2
投票

您需要对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");
}

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

© www.soinside.com 2019 - 2024. All rights reserved.