【问题标题】:EWS Managed API Timezone "Unable to convert"EWS 托管 API 时区“无法转换”
【发布时间】:2014-05-28 10:28:01
【问题描述】:

我使用 EWS Managed API 2.1 并使用在我的 Win 2008 R2(德语)IIS 服务器上找到的所有系统时区初始化我的 ExchangeService,并使用最新的补丁级别:

EWS ews;
string s = "";
foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones()) {
  try {
    ews = new ExchangeService(ExchangeVersion.Exchange2010, tz);
    ews.Credentials = new WebCredentials("alex@contoso.com", "password");
    ews.AutodiscoverUrl("alex@contoso.com");
    Appointment app = new Appointment(ews);
    app.Start = DateTime.Now;
    app.End = DateTime.Now.AddMinutes(15);
    app.Subject = tz.Id;
    app.Save();
  } catch(Exception ex) { s += ex.Message + "\n"; }
}

我收到一些时区错误。错误消息是:

Unable to convert 2009-01-01T00:00:00.000 from (UTC-03:00) Buenos Aires to UTC.
Unable to convert 2012-01-01T00:00:00.000 from (UTC-03:00) Salvador to UTC.
Unable to convert 2012-01-01T00:00:00.000 from (UTC+02:00) Tripolis to UTC.
Unable to convert 2009-01-01T00:00:00.000 from (UTC+04:00) Port Louis to UTC.
Unable to convert 2009-01-01T00:00:00.000 from (UTC+08:00) Perth to UTC.

谁能给我解释一下,尽可能简单,

  • 为什么这些转换由 dll 代码尝试?
  • 他们为什么会失败?
  • 我是否/如何避免这个问题?

【问题讨论】:

  • 当您的代码使用 DateTime.Now 时,您如何收到 2009 年和 2012 年的错误消息?
  • @MattJohnson 这正是我想知道的......

标签: c# timezone exchangewebservices ews-managed-api


【解决方案1】:

这只是一个猜测,但可能是由于 DateTime.Now 具有“本地”种类,而您使用的是不同的时区。考虑指定目标时区的时间:

var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
app.Start = now;
app.End = now.AddMinutes(15);

您可能还想明确指定开始和结束时区。我不确定这是否重要,但您可以测试:

app.StartTimeZone = tz;
app.EndTimeZone = tz;

抱歉,我没有方便的 Exchange 服务器来测试这些假设,但您可以尝试看看它是否适合您。

【讨论】:

    【解决方案2】:

    这是 .Net https://connect.microsoft.com/VisualStudio/feedback/details/1027179/timezone-conversion-bug 中的一个错误

    我使用了这个解决方法

            var timeZoneInfo = GetWorkaroundTimeZone(TimeZoneInfo.Local);
            var appointment = new Appointment(service)
                              {
                                  Subject = subject,
                                  Body = body,
                                  Start = start,
                                  StartTimeZone =  timeZoneInfo,
                                  End = end,
                                  EndTimeZone = timeZoneInfo
                              };
    
        private static TimeZoneInfo GetWorkaroundTimeZone(TimeZoneInfo timeZone)
        {
            try
            {
                TimeZoneInfo.ConvertTime(new DateTime(2014, 1, 1), timeZone, TimeZoneInfo.Utc);
                TimeZoneInfo.ConvertTime(new DateTime(2012, 1, 1), timeZone, TimeZoneInfo.Utc);
                TimeZoneInfo.ConvertTime(new DateTime(2009, 1, 1), timeZone, TimeZoneInfo.Utc);
                return TimeZoneInfo.Local;
            }
            catch (Exception ex)
            {
                return TimeZoneInfo.CreateCustomTimeZone(
                    "Time zone to workaround bug",
                    timeZone.BaseUtcOffset,
                    "Time zone to workaround bug",
                    "Time zone to workaround bug");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多