我正在使用.NET Core 3.1。我想知道是否HttpContext.Session.SetString(...)是线程安全的?例如,客户端同时向同一个控制器操作 ( Test) 发出两个请求。控制器向会话中添加一个字符串(参见下面的示例)。会话结束时是否会有两个、一个或零个键(例如,当我刷新页面时)?
public IActionResult Test()
{
HttpContext.Session.SetString(Guid.NewGuid().ToString(), "test");
return Ok();
}
使用DevExtreme FileUploader时,我将一些值保存到会话中。当我一次上传多个文件时,组件同时发出多个请求,最后,会话中通常缺少一些键。我认为有一些比赛条件正在发生。
添加:客户代码
我注意到只有在我使用时才会丢失会话密钥method: 'POST'(只有 1 个密钥)。如果我使用method: 'GET',则有 3 个键(正确)。
$(document).ready(function () {
var method = 'GET'; // works (3 keys)
//var method = 'POST'; // doesn't work (1 key)
$('#fire').on('click', function () {
$.when(
$.ajax({
url: 'Session/Test',
method: method,
success: function(){
console.log('response', 1);
}
}),
$.ajax({
url: 'Session/Test',
method: method,
success: function(){
console.log('response', 2);
}
}),
$.ajax({
url: 'Session/Test',
method: method,
success: function(){
console.log('response', 3);
}
})
).then(function () {
alert('Done');
});
});
});