我在运行几个网站的服务器上的 DateTime.Now 上抛出异常。在过去的 3 天里,这在我身上发生了两次。真的很奇怪。我想知道最新的 Windows 更新是否已经开始发生这种情况,以及你们中是否有人看到类似的行为出现。
抛出的异常是:
BASE EXCEPTION:
TYPE: System.ArgumentOutOfRangeException
MESSAGE: Value to add was out of range.
Parameter name: value
STACK TRACE:
at System.DateTime.Add(Double value, Int32 scale)
at System.TimeZoneInfo.TransitionTimeToDateTime(Int32 year, TransitionTime transitionTime)
at System.TimeZoneInfo.GetDaylightTime(Int32 year, AdjustmentRule rule)
at System.TimeZoneInfo.GetIsDaylightSavingsFromUtc(DateTime time, Int32 Year, TimeSpan utc, AdjustmentRule rule, Boolean& isAmbiguousLocalDst)
at System.TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(DateTime time, Boolean& isAmbiguousLocalDst)
at System.DateTime.get_Now()
at (my code).FrontEnd.FrontEndPage.Page_Load(Object sender, EventArgs e) in (my code file)\code\presentation\FrontEndPage.cs:line 118
at (my code).purchase.Page_Load(Object sender, EventArgs e) in (my code file)\purchase.aspx.cs:line 94
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
发生这种情况的代码是 if 语句中的第一行:
HttpCookie loggedIn = Request.Cookies[Config.Instance.LoggedInCookieName];
if (loggedIn != null)
{
loggedIn.Expires = DateTime.Now.AddHours(4);
Response.Cookies.Add(loggedIn);
}
虽然那里有一个 AddHours 并且异常是在谈论 DateTime.Add,但我认为它与 AddHours 没有任何关系,而是由对 Now 的调用引起的,正如您在堆栈跟踪中看到的那样。
我所在的服务器运行的是 Windows Server 2003,并且运行的是英语(英国)语言环境。
谢谢你的帮助。