1. 程式人生 > 其它 >donet5在centos的時區問題

donet5在centos的時區問題

在windows下開發程式,hangfire中用到了時區,採用的是本地時區:

TimeZoneInfo zone = TimeZoneInfo.Local;

RecurringJob.AddOrUpdate("任務", () => job.ExecuteAsync(), "0 0 23 * * ?", zone);

結果部署在centos中時提示以下錯誤:

System.InvalidOperationException: Recurring job can't be scheduled, see inner exception for details.
 ---> System.TimeZoneNotFoundException: The time zone ID 'China Standard Time' was not found on the local computer.
 ---> System.IO.FileNotFoundException: Could not find file '/usr/share/zoneinfo/China Standard Time'.
File name: '/usr/share/zoneinfo/China Standard Time'
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
   at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at Internal.IO.File.ReadAllBytes(String path)
   at System.TimeZoneInfo.TryGetTimeZoneFromLocalMachine(String id, TimeZoneInfo& value, Exception& e)
   --- End of inner exception stack trace ---
   at System.TimeZoneInfo.FindSystemTimeZoneById(String id)
   at Hangfire.RecurringJobEntity..ctor(String recurringJobId, IDictionary`2 recurringJob, ITimeZoneResolver timeZoneResolver, DateTime now)
   --- End of inner exception stack trace ---
   at Hangfire.Server.RecurringJobScheduler.ScheduleRecurringJob(BackgroundProcessContext context, IStorageConnection connection, String recurringJobId, RecurringJobEntity recurringJob, DateTime now)

後來寫一個測試程式將windows10和Centos的時區ID和名稱列舉出來:
Window10的時區共計141個:

cnetos的時區,共425個:

因此最後將程式碼改為如下,部署後執行成功:

TimeZoneInfo zone = TZConvert.GetTimeZoneInfo("Asia/Shanghai");
RecurringJob.AddOrUpdate("任務", () => job.ExecuteAsync(), "0 0 23 * * ?", zone);