简体   繁体   English

Azure web asp.net webapi 中的应用程序时区错误

[英]Azure web app timezone error in asp.net webapi

I have web app in azure paas environment.我在 azure paas 环境中有 web 应用程序。 I need to convert the time in different timezone, i have following code which run perfectly fine on dev machine but when i deploy on azure paas environment it throw error我需要转换不同时区的时间,我有以下代码在开发机器上运行得很好但是当我在 azure paas 环境上部署时它会抛出错误

TimeZoneInfo serverTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);
                return TimeZoneInfo.ConvertTimeToUtc(_lastUpdatedDate.Value, serverTimeZoneInfo);

First line throw exception.第一行抛出异常。 Error getting value from 'DateCreated' on 'ViewModels.Orders.OrderActivityViewModel'从“ViewModels.Orders.OrderActivityViewModel”上的“DateCreated”获取值时出错

A few things:一些东西:

  • Don't use the TimeZone class.不要使用TimeZone It is obsolete.它已经过时了。 Use only the TimeZoneInfo class.仅使用TimeZoneInfo class。
  • The StandardName is NOT the same as the Id for all cases.对于所有情况, StandardName都与Id不同。 It is also impacted by OS language, where the Id is not.它还受操作系统语言的影响,而Id则不受此影响。
  • The local time zone's Id is at TimeZoneInfo.Local.Id , though there's no point in finding it by Id when you already have it.本地时区的 Id 位于TimeZoneInfo.Local.Id ,尽管当您已经拥有它时,通过 Id 找到它是没有意义的。 You'd just use TimeZoneInfo.Local .您只需使用TimeZoneInfo.Local
  • If you're just converting local time to UTC, you don't need to mess with time zones at all, just use .ToUniversalTime() on your original DateTime or DateTimeOffset value.如果您只是将本地时间转换为 UTC,则根本不需要弄乱时区,只需在原始DateTimeDateTimeOffset值上使用.ToUniversalTime()
  • The server's time zone probably already is UTC.服务器的时区可能已经是 UTC。 That is the default in Azure anyway.无论如何,这是 Azure 中的默认设置。 Thus it isn't going to do anything at all.因此它根本不会做任何事情。
  • If necessary, you can change Azure App Service's WEBSITE_TIME_ZONE setting (on Windows only), but I don't recommend that.如有必要,您可以更改 Azure 应用服务的WEBSITE_TIME_ZONE设置(仅在 Windows 上),但我不建议这样做。 If you have a specific time zone in mind, then you would pass that ID into TimeZoneInfo.FindSystemTimeZoneById .如果您有特定的时区,则可以将该 ID 传递给TimeZoneInfo.FindSystemTimeZoneById
  • As others pointed out, the error you reported isn't in the code you gave, so likely none of this is going to help you.正如其他人指出的那样,您报告的错误不在您提供的代码中,因此这些可能都对您没有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM