我们有一个运行ASP.NET网站,它引发了一个NullReference-exception和一个根本不可能的堆栈跟踪和行号。我自己也无法从头到尾做出决定。Inexplicable NullReferenceException仅在生产中发生
它说:
Exception at ReportService.GetReport(String reportType) in ReportService.cs:line 1458
这是有趣的,因为这是该行:
var exports = new List<ReportExport>();
多亏了(很短)堆栈跟踪,我可以看到错误被触发GetReport函数,而不是在“GetAllUsers”或“GetAllUsersWithFilter”函数中,因为我会在我的电子邮箱中收到不同的错误消息,或者我会在堆栈跟踪中看到它。
所以我怀疑的行号是错误的,在这种情况下,只有一个另一种可能性,那就是这条线:
foreach (var userProfile in users) {
exports.Add(CreateUserProfile(userProfile));
}
却怎么users
永远是空?
满(虽然简单)的代码就在这里:
public function IList<ReportExport> GetReport(string reportType) {
try {
IQueryable<UserProfile> users = null;
switch (reportType) {
case "abc" :
users = GetAllUsersWithFilter();
break;
case default:
users = GetAllUsers();
break;
}
var exports = new List<ReportExport>();
foreach (var userProfile in users) {
exports.Add(CreateUserProfile(userProfile));
}
} catch (Exception ex) {
SendErrorMail("GetReport has failed", ex); /* I receive this error mail */
}
function IQueryable<UserProfile> GetAllUsers() {
try {
return dbContext.Users.Where(x => x.IsRegistered == true);
} catch (Exception ex) {
SendErrorMail("GetAllUsers", ex); /* I don't receive this e-mail */
return null;
}
}
function IQueryable<UserProfile> GetAllUsersWithFilter() {
try {
return GetAllUsers().Where(x => x.ExtraFilter == true);
} catch (Exception ex) {
SendErrorMail("GetAllUsersWithFilter", ex); /* I don't receive this e-mail */
}
}
function int GetNumberOfSessions(int userId) {
try {
return dbContext.Sessions.Count(x => x.UserId == userId);
} catch (Exception ex) {
SendErrorMail("GetNumberOfSessions", ex); /* I don't receive this e-mail */
}
}
function ReportExport CreateUserExport(UserProfile user) {
try {
var cnt = GetNumberOfSessions(user.Id);
return new ReportExport() {
UserId = user.Id,
NumberOfSessions = cnt
}
} catch (Exception ex) {
SendErrorMail(("CreateUserExport", ex);
}
}
如果您正在生产中,那么您可能在开启优化时运行 - 因此行号会出错。但是你正在捕获Exception并返回null。你依靠返回数据 - 这可能不是真正的GetAllUsers – Andez
的情况,但我会得到另一个不同的堆栈跟踪错误邮件,不是吗? –
SendErrorMail如何处理多个传入消息?它可以隐藏你吗? – Dawnkeeper