关于如何解决分布式系统中的跨时区问题,上一篇详细介绍了解决方案的实现原理,在这一篇中我们通过一个完整的例子来对这个问题进行深入探讨。尽管《原理篇》中介绍了那么多,解决方案的本质就是:在进行服务调用过程中将客户端的时区信息作为上下文传入服务端,并以此作为时间转换的依据。我们首先定一个具体的类型来定义包含时区信息的上下文类型,我们将这个类型起名为ApplicationContext。

在《通过WCF扩展实现Context信息的传递》一文中,我通过HttpSessionState和CallContext实现了一个ApplicationContext类,为ASP.NET和其他类型的应用提供上下文信息的容器。在这里进行了简化,仅仅实现了基于CallContext的部分。这样一个ApplicationContext类型定义如下:

)]
object>
   3: {
;
;
   6:  
private ApplicationContext() { }
static ApplicationContext Current
   9:     {
  10:         get
  11:         {
typeof(ApplicationContext).FullName)) 
  13:             {
typeof(ApplicationContext))
  15:                 {
typeof(ApplicationContext).FullName))
  17:                     {
new ApplicationContext();
  19:                         context.TimeZone = TimeZoneInfo.Local;
typeof(ApplicationContext).FullName, context);
  21:                     }
  22:                 }
  23:             }
  24:  
typeof(ApplicationContext).FullName);
  26:         }
  27:         set
  28:         {
value);
  30:         }
  31:     }
public TimeZoneInfo TimeZone
  33:     {
  34:         get
  35:         {
]);
  37:         }
  38:         set
  39:         {
value.ToSerializedString();
  41:         }
  42:     }
  43:  
void Clear()
  45:     { 
typeof(ApplicationContext).FullName);
  47:     }
  48: }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2021-10-15
  • 2021-09-25
  • 2021-10-22
猜你喜欢
  • 2021-08-24
  • 2021-05-27
  • 2021-09-23
  • 2022-12-23
  • 2021-12-07
  • 2022-01-01
相关资源
相似解决方案