为什么我的方法被 Page_Load() 调用两次(或更多次)
Why is my method being called twice (or more) from Page_Load()
(这是我几个小时前问的一个问题的转贴,由于不够详细而被关闭)
我有一个导入例程,它从 json 文件中获取内容并使用 JsonConvert.Deserialize() 将内容读入名为“items”的动态变量中。这是代码片段(为简洁起见删除了一些内容 - 这个例程有 800 行长....)
它是由 Page_Load 事件触发的;
protected void Page_Load(object sender, EventArgs e)
{
if (Helpers.RequestString("task") == "importData") // Helpers is a function I have created to sanitize query string input
{
ImportData_new(); // this is the process being called
}
}
我在此过程中附加到 StringBuilder 对象,然后在循环遍历 json.
中的项目结束时将字符串生成器的内容写入日志文件
现在方法本身;
private void ImportData_new()
{
_importGroup = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
_logOutput.Clear();
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "###################### INITIALISING IMPORT ROUTINE #######################", Environment.NewLine));
//File.AppendAllText(_logPath, _logOutput.ToString());
var OK = true;
// Grab the text file contents
var file_content = "";
try
{
file_content = System.IO.File.ReadAllText(_filePath + "testjson.json");
}
catch (Exception e)
{
OK = false;
//_logOutput.Clear();
_logOutput.Append(string.Format("{0}: {1}{2}{3}################# IMPORT ABORTED #################{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Import aborted: Unable to read text from text file. Error: ", e.Message, Environment.NewLine));
File.AppendAllText(_logPath, _logOutput.ToString());
}
var file_content = System.IO.File.ReadAllText(_filePath + "testjson.json");
dynamic items = JsonConvert.DeserializeObject(file_content);
foreach (dynamic item in items){
// read values from the "item" dynamic object and use them to create a database record/row
// Here's one example of the numerous sections I am processing
var DayNightValue = item["TabA OperationalHours"] != null && item["TabA OperationalHours"].Value != null && item["TabA OperationalHours"].Value != "" && item["TabA OperationalHours"].Value.ToString().Trim() != "" ? item["TabA OperationalHours"].Value.ToString().Trim() : "";
if (DayNightValue != "")
{
if (DayNightValue.ToString().ToLower().StartsWith("day"))
{
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Item " + counter + ": Day/Night: Day", Environment.NewLine));
hems.DayNightVal = "Day";
}
if (DayNightValue.ToString().ToLower().StartsWith("night"))
{
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Item " + counter + ": Day/Night: Night", Environment.NewLine));
hems.DayNightVal = "Night";
}
}
else
{
hems.DayNightVal = null;
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "## WARNING for Item " + counter + ": Day/Night value is empty. Setting as null.", Environment.NewLine));
}
...process the rest of the properties in the json item
} // end of the for loop.
}
....<snipped>
例程结束时,我将日志信息输出到一个文本文件。日志文件显示一个循环是 运行 但在它完成之前另一个循环开始,因此日志文件显示一个循环与另一个循环混合的信息。每次导入应该有 3 条记录写入日志(仅测试 3 条记录)。但是正在发生的事情是例程开始一个导入,但随后似乎与它一起发生了一个二次导入 - 见下文(我的笔记在大括号中,用方括号括起来)。请注意该行的乱序写入(请参阅时间戳)。
[**IMPORT STARTS**]
2020-08-06 13:18:12: ###################### INITIALISING IMPORT ROUTINE #######################
2020-08-06 13:18:12: 3 rows found.....
2020-08-06 13:18:12: Importing Item 1: Flight Record: xxxx, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 343, Patient: xxxx
2020-08-06 13:18:12: Item 1: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:12: Item 1: Service Item Code Value: (Supplied: ETA11) Actual ID:6888
2020-08-06 13:18:12: Item 1: Day/Night: Day
2020-08-06 13:18:12: Item 1: Reason for use of Helicopter (Value: Multi reasons), actual ID: 8.
2020-08-06 13:18:12: Item 1: Mission Type Value: (Supplied: Pre Hospital Trauma) Actual ID:6906
2020-08-06 13:18:12: Item 1: Patient Outcome Value: (Supplied: Life saved) Actual ID:7134
[**......SNIPPED SOME OF THE ROWS**]
[**LOG GOES THROUGH FIRST ITEM IN IMPORT BUT THEN INSTEAD OF SHOWING THE NEXT TWO ITEMS, SHOWS THE FINISHED NOTIFICATION**]
2020-08-06 13:18:12: ############ IMPORT FINISHED ############
[**THEN SHOWS ANOTHER ONE STARTING - NOTE IT SAYS "Item 1" again...**]
2020-08-06 13:18:12: ###################### INITIALISING IMPORT ROUTINE #######################
2020-08-06 13:18:12: 3 rows found.....
2020-08-06 13:18:12: Importing Item 1: Flight Record: 5676, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 343, Patient: xxxx
2020-08-06 13:18:12: Item 1: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:12: Item 1: Service Item Code Value: (Supplied: ETA11) Actual ID:6888
2020-08-06 13:18:12: Item 1: Day/Night: Day
[**......SNIPPED SOME OF THE ROWS - SAYS IT'S FINISHED AFTER 1 ITEM AGAIN**]
2020-08-06 13:18:12: ############ IMPORT FINISHED ############
[**NOW THE TIME IN THE LOG CHANGES TO 27 SECONDS AFTER THE IMPORT STARTED - BUT THIS TIME SEEMINGLY STARTING ON Item 2**]
2020-08-06 13:18:29: Importing Item 2: Flight Record: 5678, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 474, Patient: xxxx
2020-08-06 13:18:29: Item 2: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:29: Item 2: Service Item Code Value: (Supplied: METAT11) Actual ID:6902
2020-08-06 13:18:29: Item 2: Day/Night: Night
[**......SNIPPED SOME OF THE ROWS**]
[**FINISHES AFTER ITEM 2 THIS TIME**]
2020-08-06 13:18:29: ############ IMPORT FINISHED ############
2020-08-06 13:18:12: ###################### INITIALISING IMPORT ROUTINE #######################
2020-08-06 13:18:12: 3 rows found.....
[**IMPORT STARTS AGAIN - AND NOTE THE TIME STAMP HAS GONE IN REVERSE**]
[**FINISHES AGAIN AFTER ONLY ONE ITEM**]
2020-08-06 13:18:12: ############ IMPORT FINISHED ############
[**NOTE THE TIME HAS CHANGED YET AGAIN**]
2020-08-06 13:18:29: Importing Item 2: Flight Record: 5678, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 474, Patient: xxxx
2020-08-06 13:18:29: Item 2: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:29: Item 2: Service Item Code Value: (Supplied: METAT11) Actual ID:6902
2020-08-06 13:18:29: Item 2: Day/Night: Night
2020-08-06 13:18:29: Item 2: Reason for use of Helicopter (Value: Time), actual ID: 3.
2020-08-06 13:18:29: Item 2: Mission Type Value: (Supplied: IHT) Actual ID:6908
2020-08-06 13:18:29: Item 2: Patient Outcome Value: (Supplied: Further complications avoided) Actual ID:6973
[**CONTINUES TILL DONE - BUT ERRORS ON THE BROWSER PAGE AS IT WOULD SEEM THAT TWO THREADS ARE ATTEMPTING TO OPEN THE LOGGING TXT FILE AT ONCE**]
因此,看起来确实有至少两个(可能更多,查看日志作为指示)进程 运行 ImportData_new() 方法。这里的 Page_Load 会不会有问题?它是调用该方法的唯一进程。
悬崖
以防万一以后有人看到这个。这就是我的问题所在。
- 我正在使用 Visual Studio 的内置网络服务器 (Cassini)
- 我正在使用 'dynamic' 变量来读取我正在接收的 JSON 数据,然后循环遍历该动态
- 我将调试器更改为使用 IIS 而不是 Cassini。这暂时解决了这个问题(我知道 Cassini 的内存分配 space 比 IIS 少 - 显然)
- 似乎使用动态变量类型会导致某种形式的内存 over-run 并且它使调试器因 Whosebug 条件而崩溃。
- 将我的动态变量更改为 class-mapped JSON ar运行gement 似乎解决了这个问题。
我不确定 'why' 情况是否如此,因为即使 JSON 对象中只有一项,动态也会导致计算器溢出。一旦我离开动态,它 运行 很好 - 并且在过去 3 周 运行 很好。
希望此信息对某人有所帮助。
(这是我几个小时前问的一个问题的转贴,由于不够详细而被关闭)
我有一个导入例程,它从 json 文件中获取内容并使用 JsonConvert.Deserialize() 将内容读入名为“items”的动态变量中。这是代码片段(为简洁起见删除了一些内容 - 这个例程有 800 行长....)
它是由 Page_Load 事件触发的;
protected void Page_Load(object sender, EventArgs e)
{
if (Helpers.RequestString("task") == "importData") // Helpers is a function I have created to sanitize query string input
{
ImportData_new(); // this is the process being called
}
}
我在此过程中附加到 StringBuilder 对象,然后在循环遍历 json.
中的项目结束时将字符串生成器的内容写入日志文件现在方法本身;
private void ImportData_new()
{
_importGroup = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
_logOutput.Clear();
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "###################### INITIALISING IMPORT ROUTINE #######################", Environment.NewLine));
//File.AppendAllText(_logPath, _logOutput.ToString());
var OK = true;
// Grab the text file contents
var file_content = "";
try
{
file_content = System.IO.File.ReadAllText(_filePath + "testjson.json");
}
catch (Exception e)
{
OK = false;
//_logOutput.Clear();
_logOutput.Append(string.Format("{0}: {1}{2}{3}################# IMPORT ABORTED #################{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Import aborted: Unable to read text from text file. Error: ", e.Message, Environment.NewLine));
File.AppendAllText(_logPath, _logOutput.ToString());
}
var file_content = System.IO.File.ReadAllText(_filePath + "testjson.json");
dynamic items = JsonConvert.DeserializeObject(file_content);
foreach (dynamic item in items){
// read values from the "item" dynamic object and use them to create a database record/row
// Here's one example of the numerous sections I am processing
var DayNightValue = item["TabA OperationalHours"] != null && item["TabA OperationalHours"].Value != null && item["TabA OperationalHours"].Value != "" && item["TabA OperationalHours"].Value.ToString().Trim() != "" ? item["TabA OperationalHours"].Value.ToString().Trim() : "";
if (DayNightValue != "")
{
if (DayNightValue.ToString().ToLower().StartsWith("day"))
{
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Item " + counter + ": Day/Night: Day", Environment.NewLine));
hems.DayNightVal = "Day";
}
if (DayNightValue.ToString().ToLower().StartsWith("night"))
{
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Item " + counter + ": Day/Night: Night", Environment.NewLine));
hems.DayNightVal = "Night";
}
}
else
{
hems.DayNightVal = null;
_logOutput.Append(string.Format("{0}: {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "## WARNING for Item " + counter + ": Day/Night value is empty. Setting as null.", Environment.NewLine));
}
...process the rest of the properties in the json item
} // end of the for loop.
}
....<snipped>
例程结束时,我将日志信息输出到一个文本文件。日志文件显示一个循环是 运行 但在它完成之前另一个循环开始,因此日志文件显示一个循环与另一个循环混合的信息。每次导入应该有 3 条记录写入日志(仅测试 3 条记录)。但是正在发生的事情是例程开始一个导入,但随后似乎与它一起发生了一个二次导入 - 见下文(我的笔记在大括号中,用方括号括起来)。请注意该行的乱序写入(请参阅时间戳)。
[**IMPORT STARTS**]
2020-08-06 13:18:12: ###################### INITIALISING IMPORT ROUTINE #######################
2020-08-06 13:18:12: 3 rows found.....
2020-08-06 13:18:12: Importing Item 1: Flight Record: xxxx, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 343, Patient: xxxx
2020-08-06 13:18:12: Item 1: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:12: Item 1: Service Item Code Value: (Supplied: ETA11) Actual ID:6888
2020-08-06 13:18:12: Item 1: Day/Night: Day
2020-08-06 13:18:12: Item 1: Reason for use of Helicopter (Value: Multi reasons), actual ID: 8.
2020-08-06 13:18:12: Item 1: Mission Type Value: (Supplied: Pre Hospital Trauma) Actual ID:6906
2020-08-06 13:18:12: Item 1: Patient Outcome Value: (Supplied: Life saved) Actual ID:7134
[**......SNIPPED SOME OF THE ROWS**]
[**LOG GOES THROUGH FIRST ITEM IN IMPORT BUT THEN INSTEAD OF SHOWING THE NEXT TWO ITEMS, SHOWS THE FINISHED NOTIFICATION**]
2020-08-06 13:18:12: ############ IMPORT FINISHED ############
[**THEN SHOWS ANOTHER ONE STARTING - NOTE IT SAYS "Item 1" again...**]
2020-08-06 13:18:12: ###################### INITIALISING IMPORT ROUTINE #######################
2020-08-06 13:18:12: 3 rows found.....
2020-08-06 13:18:12: Importing Item 1: Flight Record: 5676, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 343, Patient: xxxx
2020-08-06 13:18:12: Item 1: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:12: Item 1: Service Item Code Value: (Supplied: ETA11) Actual ID:6888
2020-08-06 13:18:12: Item 1: Day/Night: Day
[**......SNIPPED SOME OF THE ROWS - SAYS IT'S FINISHED AFTER 1 ITEM AGAIN**]
2020-08-06 13:18:12: ############ IMPORT FINISHED ############
[**NOW THE TIME IN THE LOG CHANGES TO 27 SECONDS AFTER THE IMPORT STARTED - BUT THIS TIME SEEMINGLY STARTING ON Item 2**]
2020-08-06 13:18:29: Importing Item 2: Flight Record: 5678, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 474, Patient: xxxx
2020-08-06 13:18:29: Item 2: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:29: Item 2: Service Item Code Value: (Supplied: METAT11) Actual ID:6902
2020-08-06 13:18:29: Item 2: Day/Night: Night
[**......SNIPPED SOME OF THE ROWS**]
[**FINISHES AFTER ITEM 2 THIS TIME**]
2020-08-06 13:18:29: ############ IMPORT FINISHED ############
2020-08-06 13:18:12: ###################### INITIALISING IMPORT ROUTINE #######################
2020-08-06 13:18:12: 3 rows found.....
[**IMPORT STARTS AGAIN - AND NOTE THE TIME STAMP HAS GONE IN REVERSE**]
[**FINISHES AGAIN AFTER ONLY ONE ITEM**]
2020-08-06 13:18:12: ############ IMPORT FINISHED ############
[**NOTE THE TIME HAS CHANGED YET AGAIN**]
2020-08-06 13:18:29: Importing Item 2: Flight Record: 5678, Mission Date: 4/08/2020 12:00:00 AM, Mission Number: 474, Patient: xxxx
2020-08-06 13:18:29: Item 2: Mission Authorised By Airdesk (Value Yes).
2020-08-06 13:18:29: Item 2: Service Item Code Value: (Supplied: METAT11) Actual ID:6902
2020-08-06 13:18:29: Item 2: Day/Night: Night
2020-08-06 13:18:29: Item 2: Reason for use of Helicopter (Value: Time), actual ID: 3.
2020-08-06 13:18:29: Item 2: Mission Type Value: (Supplied: IHT) Actual ID:6908
2020-08-06 13:18:29: Item 2: Patient Outcome Value: (Supplied: Further complications avoided) Actual ID:6973
[**CONTINUES TILL DONE - BUT ERRORS ON THE BROWSER PAGE AS IT WOULD SEEM THAT TWO THREADS ARE ATTEMPTING TO OPEN THE LOGGING TXT FILE AT ONCE**]
因此,看起来确实有至少两个(可能更多,查看日志作为指示)进程 运行 ImportData_new() 方法。这里的 Page_Load 会不会有问题?它是调用该方法的唯一进程。
悬崖
以防万一以后有人看到这个。这就是我的问题所在。
- 我正在使用 Visual Studio 的内置网络服务器 (Cassini)
- 我正在使用 'dynamic' 变量来读取我正在接收的 JSON 数据,然后循环遍历该动态
- 我将调试器更改为使用 IIS 而不是 Cassini。这暂时解决了这个问题(我知道 Cassini 的内存分配 space 比 IIS 少 - 显然)
- 似乎使用动态变量类型会导致某种形式的内存 over-run 并且它使调试器因 Whosebug 条件而崩溃。
- 将我的动态变量更改为 class-mapped JSON ar运行gement 似乎解决了这个问题。
我不确定 'why' 情况是否如此,因为即使 JSON 对象中只有一项,动态也会导致计算器溢出。一旦我离开动态,它 运行 很好 - 并且在过去 3 周 运行 很好。
希望此信息对某人有所帮助。