【发布时间】:2021-03-25 14:10:59
【问题描述】:
我有一个使用 Azure Active Directory 和 Azure SQL Server 在 Azure 中运行的 ASP.NET WEBAPI 服务。 我已进入 Azure App Services 设置并开启 Always On,但我确信 WEBAPI 正在卸载。
为了测试,我有一组通常需要 4-5 秒的调用。有时通话时间超过 30 秒。
除了这种偶尔的缓慢响应之外,没有任何问题。
WEBAPI 服务没有可查看的页面。仅限 API 控制器。
可能不相关,但我也使用 EF 进行数据访问。我也使用 CORS。
这是我的 Azure 应用设置:
[
{
"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
}
]
这是我的大部分 StartUp.cs 文件:
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();
});
}
【问题讨论】:
-
如果我的解决方案对您有帮助,请您将我的答案标记为accepted,tks~
-
我看到类似的情况,你有什么运气吗?
标签: azure azure-web-app-service asp.net-core-webapi