0

I have an ASP.NET WEBAPI service running in Azure using Azure Active Directory and Azure SQL Server. I have gone into Azure App Services settings and turned on Always On, but I am sure the WEBAPI is unloading.

For testing I have a set of calls that usually take 4-5 seconds. Sometimes the calls take over 30 seconds.

Other than this occasional slow response, there are no issues.

The WEBAPI service has no viewable pages. API controllers only.

Probably not relevant, but I also use EF for data access. I also use CORS.

Here are my Azure App Settings:

[
  {
    "name": "ASPNETCORE_ENVIRONMENT",
    "value": "Development",
    "slotSetting": true
  },
  {
    "name": "BasePath",
    "value": "*****************************",
    "slotSetting": true
  },
  {
    "name": "WEBSITE_HTTPLOGGING_RETENTION_DAYS",
    "value": "7",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_RUN_FROM_PACKAGE",
    "value": "1",
    "slotSetting": false
  }
]

Here is the Azure General Settings: General Settings

Here is most of my StartUp.cs file:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DatabaseContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection"), o =>
            {
                o.EnableRetryOnFailure();
            })
        .ConfigureWarnings(warnings => warnings.Ignore(Microsoft.EntityFrameworkCore.Diagnostics.CoreEventId.ContextInitialized))
        .EnableSensitiveDataLogging(true));

    services
        .AddAuthentication("Azure")
        .AddPolicyScheme("Azure", "Authorize AzureAd or AzureAdBearer", options =>
        {
            options.ForwardDefaultSelector = context =>
            {
                var authHeader = context.Request.Headers["Authorization"].FirstOrDefault();
                if (authHeader?.StartsWith("Bearer") == true)
                {
                    return JwtBearerDefaults.AuthenticationScheme;
                }
                return AzureADDefaults.AuthenticationScheme;
            };
        })
        .AddJwtBearer(opt =>
        {
            opt.Audience = Configuration["AAD:ResourceId"];
            opt.Authority = $"{Configuration["AAD:Instance"]}{Configuration["AAD:TenantId"]}";
        })
        .AddAzureAD(options => Configuration.Bind("AzureAd", options));


    var origins = Configuration.GetSection("AppSettings:AllowedOrigins").Value.Split(",");
    services.AddCors(o => o.AddPolicy(specificOrigins, builder =>
    {
        builder.WithOrigins(origins)
               .AllowAnyMethod()
               .AllowAnyHeader()
               .AllowCredentials()
               .SetIsOriginAllowed((host) => true);
    }));

    services.Configure<CookiePolicyOptions>(options =>
    {
        options.MinimumSameSitePolicy = SameSiteMode.Strict;
        options.HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always;
        options.Secure = CookieSecurePolicy.Always;
    });
    services.AddResponseCaching();

    services.AddControllers();

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddScoped<LogUserActivity>();
    services.AddSession();
    services.AddHttpContextAccessor();
    services.AddAutoMapper();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    loggerFactory.AddSerilog();
    if (_currentEnvironment.IsDevelopment() || _currentEnvironment.IsEnvironment("LOCAL"))
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
    app.UseCors(specificOrigins);
    app.UseHttpsRedirection();
    app.UseSession();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseResponseCaching();

    app.UseCookiePolicy();
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapDefaultControllerRoute();
    });
}
Bobby Ortiz
  • 3,077
  • 7
  • 35
  • 45

1 Answers1

1

The answer to this question should be the process of testing.

According to the official explanation, we can see.

enter image description here

Test Steps

  1. Create a test api, like below for test.

    public string test() {
        return "I'm test api.";
    }
    
  2. Insert code that calculates the execution time of each step or line of code

    Eg: Using Stopwatch in C#

The Reason

The official always on should be no problem. It is recommended to pass the test code, and test each step takes a long time.

Then the tested api is used to test whether the phenomenon you mentioned will still occur when this simple code is executed under the same conditions.

The possible cause of the problem is to connect to the database to execute SQL, or other business takes a long time, or other errors. Record the time-consuming time or view it through ILogger output.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29