Azure中ASP.NET核心WEBAPI服务的初始响应缓慢-始终打开已为真
我使用Azure Active Directory和Azure SQL Server在Azure中运行ASP.NET WEBAPI服务。 我已进入Azure应用程序服务设置并始终打开,但我确信WEBAPI正在卸载 对于测试,我有一组通常需要4-5秒的调用。有时通话时间超过30秒 除了偶尔的缓慢反应,没有其他问题 WEBAPI服务没有可查看的页面。仅限API控制器 可能不相关,但我也使用EF进行数据访问。我也使用CORS 以下是我的Azure应用程序设置:Azure中ASP.NET核心WEBAPI服务的初始响应缓慢-始终打开已为真,azure,azure-web-app-service,asp.net-core-webapi,Azure,Azure Web App Service,Asp.net Core Webapi,我使用Azure Active Directory和Azure SQL Server在Azure中运行ASP.NET WEBAPI服务。 我已进入Azure应用程序服务设置并始终打开,但我确信WEBAPI正在卸载 对于测试,我有一组通常需要4-5秒的调用。有时通话时间超过30秒 除了偶尔的缓慢反应,没有其他问题 WEBAPI服务没有可查看的页面。仅限API控制器 可能不相关,但我也使用EF进行数据访问。我也使用CORS 以下是我的Azure应用程序设置: [ { "name
[
{
"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
}
]
以下是Azure常规设置:
以下是我的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();
});
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(
Configuration.GetConnectionString(“DefaultConnection”),o=>
{
o、 EnableRetryOnFailure();
})
.ConfigureWarnings(warnings=>warnings.Ignore(Microsoft.EntityFrameworkCore.Diagnostics.CoreEventId.ContextInitialized))
.EnableSensitiveDataLogging(真));
服务
.AddAuthentication(“Azure”)
.AddPolicyScheme(“Azure”、“授权AzureAd或AzureAd持有人”,选项=>
{
options.ForwardDefaultSelector=上下文=>
{
var authHeader=context.Request.Headers[“Authorization”].FirstOrDefault();
if(authHeader?.StartsWith(“承载人”)==true)
{
返回JwtBearerDefaults.AuthenticationScheme;
}
返回AzureADDefaults.AuthenticationScheme;
};
})
.AddJwtBearer(opt=>
{
opt.acquisition=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=>
{
建筑商。带原点(原点)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.SetIsOriginAllowed((主机)=>true);
}));
配置(选项=>
{
options.MinimumSameSitePolicy=SameSiteMode.Strict;
options.HttpOnly=Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always;
options.Secure=CookieSecurePolicy.Always;
});
services.addResponseCache();
services.AddControllers();
services.AddSingleton();
services.addScope();
services.AddSession();
AddHttpContextAccessor();
services.AddAutoMapper();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、ILoggerFactory loggerFactory)
{
loggerFactory.AddSerilog();
if(_currentEnvironment.IsDevelopment()|||u currentEnvironment.IsEnvironment(“本地”))
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
app.UseHsts();
}
附录UseCors(具体来源);
app.UseHttpsRedirection();
app.UseSession();
app.UseStaticFiles();
app.UseRouting();
app.UseResponseCaching();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
endpoints.MapDefaultControllerOute();
});
}
这个问题的答案应该是测试过程。
根据,我们可以看到。
测试步骤
public string test() {
return "I'm test api.";
}
问题的可能原因是连接到数据库以执行SQL,或者其他业务需要很长时间,或者其他错误。记录耗时或通过ILogger输出查看。我将尝试您的建议。我仍在测试中。你的评论很有帮助,但我仍在挖掘。如果我的解决方案对你有帮助,请你将我的答案标记为,tks~我看到类似的结果,你在这方面运气好吗?