【发布时间】:2019-01-18 23:23:48
【问题描述】:
我有一个脚本可以向包含 JSON 的端点发送多个 POST。目前,该脚本可能会同时从 localhost 发出 100 多个 POST。在请求所有 POST 之前,不会调用该路由的方法;在此之前,只有 HTTP OPTIONS(CORS 预检)作为响应发送。一旦脚本停止发出 POST 请求,服务器才会开始调用路由方法。
当然,让脚本同时发出 100 多个 POST 请求会有所改变,但我担心这将如何影响许多用户的生产使用。
在我的调试中,我发现在该批次中的所有 POST 请求都发出之前,我的过滤器/代码都不会运行。
我不知道如何解决这个问题。 CPU 和内存甚至还没有接近极限,更改过滤器顺序没有效果,优化路由方法也没有效果,因为直到所有请求都发出后才会调用它。
我正在开发机器上使用 IIS Express 10 上的 Visual Studio 进行测试。我使用的是 ASP.NET Core 2.0。
应用配置:
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(opt =>
{
opt.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
})
.AddMvcOptions(opt =>
{
opt.Filters.Add(new IPLoggingInterceptionAttribute());
});
services.AddCors(options =>
{
options.AddPolicy("AllOrigins", builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.AllowAnyOrigin();
});
});
services
.AddScoped<RequireAuthAttribute>();
String connectionString = Configuration.GetConnectionString("Vault");
services.AddDbContext<VaultContext>(options => options.UseNpgsql(connectionString));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("AllOrigins");
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All,
RequireHeaderSymmetry = false
});
app.UseMvc();
}
路由响应方法,以防相关:
[HttpPost("{villageId}/army/current")]
public async Task<IActionResult> PostCurrentArmy(long villageId, [FromBody]JSON.ArmySet currentArmySetJson)
{
if (!ModelState.IsValid)
return ValidationProblem(ModelState);
// [Note that 'CurrentWorldId' is a blocking property on first read]
// Get or make CurrentVillage
Scaffold.CurrentVillage currentVillage =
await context.CurrentVillage
.FromWorld(CurrentWorldId)
.Where(v => v.VillageId == villageId)
.FirstOrDefaultAsync();
currentVillage = currentVillage ?? (await context.AddAsync(new Scaffold.CurrentVillage())).Entity;
currentVillage.WorldId = CurrentWorldId;
if (currentVillage.PlayerId == null)
{
var modelVillage = await (
from village in context.Village.FromWorld(CurrentWorldId)
where village.VillageId == villageId
select village
).FirstOrDefaultAsync();
if (modelVillage != null)
{
currentVillage.VillageId = modelVillage.VillageId;
currentVillage.PlayerId = modelVillage.PlayerId;
currentVillage.Points = modelVillage.Points;
currentVillage.VillageName = modelVillage.VillageName;
currentVillage.X = modelVillage.X.Value;
currentVillage.Y = modelVillage.Y.Value;
}
}
if (!Configuration.Security.AllowUploadArmyForNonOwner
&& currentVillage.PlayerId != CurrentUser.PlayerId)
{
await context.InvalidDataRecord.AddAsync(MakeInvalidDataRecord(
JsonConvert.SerializeObject(currentArmySetJson),
$"Attempted to upload current army to village {villageId} but that village is not owned by the requestor"
));
await context.SaveChangesAsync();
return Unauthorized();
}
var fullArmy = currentArmySetJson.Stationed + currentArmySetJson.Traveling + currentArmySetJson.Supporting;
currentVillage.ArmyOwned = ArmyConvert.JsonToArmy(fullArmy, currentVillage.ArmyOwned, context);
currentVillage.ArmyStationed = ArmyConvert.JsonToArmy(currentArmySetJson.Stationed, currentVillage.ArmyStationed, context);
currentVillage.ArmyTraveling = ArmyConvert.JsonToArmy(currentArmySetJson.Traveling, currentVillage.ArmyTraveling, context);
await context.SaveChangesAsync();
return Ok();
}
【问题讨论】:
-
你确定它不是阻塞的java脚本/浏览器?另请注意,IIS Express 不是真正的服务器,它用于开发
-
这不应被
Core App阻止。与我们分享发送100+请求的script。
标签: c# asp.net-core cors