Reinhard在AIF中使用DateTime作为服务契约的参数,与DotNet程序进行交互时,总是因为时区的问题,导致DotNet提交的System.DateTime与AIF中接收的DateTime 不一致。

这个问题着实困扰了Reinhard 许久。为了解决这个问题,Reinhard这次专门写一篇博文,通过一系列的实验,找到通过AIF服务契约,对DateTime进行存储、获取、比较的最佳方式。

1、DateTime存储实验

在AX中,Reinhard写了三个存储DateTime方法,分别测试了System.DateTime和UtcDateTime作为服务契约参数的不同情况。这三个服务契约如下:

[SysEntryPointAttribute]

public void saveSystemDateTime(System.DateTime _dt)

{

ReinhardTest_Table table;

table.UtcDateTime=_dt;

table.insert();

}

[SysEntryPointAttribute]

public void saveSystemDateTime2(System.DateTime _dt)

{

ReinhardTest_Table table;

table.UtcDateTime=Global::clrSystemDateTime2UtcDateTime( _dt);

table.insert();

}

[SysEntryPointAttribute]

public void saveUtcDateTime( utcDateTime _utcDateTime)

{

ReinhardTest_Table table;

table.UtcDateTime=_utcDateTime;

table.insert();

}

接着,在VS中,Reinhard调用这三个服务契约,分别存储三个不同的System.DateTime实例,看哪个服务契约能够正确地存储DateTime。这三个日期分别是:

DateTime saveSystemDateTimeDT = new DateTime(2013, 3, 3, 3, 3, 3);

DateTime saveSystemDateTime2DT = new DateTime(2014, 4, 4, 4, 4, 4);

DateTime saveUtcDateTimeDT = new DateTime(2015, 5, 5, 5, 5, 5);

调用完毕后,到AX中,查看ReinhardTest_Table中保存的日期。

Reinhard发现,只有SaveUtcDateTime这个服务契约,正确保存了DotNet中System.DateTime的值。

结论:正确的日期存储方式是SaveUtcDateTime。

2、DateTime对比实验

在AX中,Reinhard写了两个比较DateTime方法,分别测试了System.DateTime和UtcDateTime作为服务契约参数的不同情况。这两个服务契约如下:

[SysEntryPointAttribute]

public boolean compareUtcDateTime(utcDateTime _utcDateTime)

{

ReinhardTest_Table table;

    select table order by table.UtcDateTime desc;

    return table.UtcDateTime==_utcDateTime;

}

[SysEntryPointAttribute]

public boolean compareSystemDateTime(System.DateTime _dt)

{

ReinhardTest_Table table;

    utcDateTime dt= Global::clrSystemDateTime2UtcDateTime(_dt);

    select table order by table.UtcDateTime desc;

    return table.UtcDateTime==dt;

}

这里,Reinhard选择了ReinhardTest_Table表中的最后一条记录,进行比较,也就是2015-05-05 05:05:05那条。

接着,在VS中,Reinhard调用这两个服务契约,与同一个System.DateTime实例做比较,这个DateTime是:

DateTime saveUtcDateTimeDT = new DateTime(2015, 5, 5, 5, 5, 5);

这个DateTime其实就是Reinhard在第一个实验中,最后一个保存的那个DateTime。不出意外的话,它应该与ReinhardTest_Table表中的最后一条记录是等价的。那么,我们来看看调用的结果吧。

Reinhard发现,只有CompareUtcDateTime这个服务契约,正确比较了DotNet中System.DateTime与AX中UtcDateTime的值。

    结论:正确的日期比较方式是CompareUtcDateTime。

3、DateTime获取实验

在AX中,Reinhard写了七个获取DateTime方法,分别测试了System.DateTime和UtcDateTime作为服务契约返回值的不同情况。这七个服务契约如下:

[SysEntryPointAttribute]

public utcDateTime getUtcDateTime()

{

ReinhardTest_Table table;

    select table order by table.UtcDateTime desc;

    return table.UtcDateTime;

}

[SysEntryPointAttribute]

public utcDateTime getUtcDateTime2()

{

ReinhardTest_Table table;

    select table order by table.UtcDateTime desc;

    return DateTimeUtil::applyTimeZoneOffset(table.UtcDateTime ,DateTimeUtil::getUserPreferredTimeZone());

}

[SysEntryPointAttribute]

public utcDateTime getUtcDateTime3()

{

ReinhardTest_Table table;

    str strDT;

    select table order by table.UtcDateTime desc;

    return DateTimeUtil::applyTimeZoneOffset(table.UtcDateTime ,DateTimeUtil::getCompanyTimeZone());

}

[SysEntryPointAttribute]

public System.DateTime getSystemDateTime()

{

ReinhardTest_Table table;

    select table order by table.UtcDateTime desc;

    return table.UtcDateTime;

}

[SysEntryPointAttribute]

public System.DateTime getSystemDateTime2()

{

ReinhardTest_Table table;

    select table order by table.UtcDateTime desc;

    return DateTimeUtil::removeTimeZoneOffset(table.UtcDateTime,DateTimeUtil::getCompanyTimeZone());

}

[SysEntryPointAttribute]

public System.DateTime getSystemDateTime3()

{

ReinhardTest_Table table;

System.DateTime dt;

    utcDateTime utcDT;

    select table order by table.UtcDateTime desc;

utcDT=DateTimeUtil::removeTimeZoneOffset(table.UtcDateTime,DateTimeUtil::getCompanyTimeZone());

dt=Global::utcDateTime2SystemDateTime(utcDT);

    return dt;

}

[SysEntryPointAttribute]

public System.DateTime getStringDateTime4()

{

ReinhardTest_Table table;

    str strDT;

    select table order by table.UtcDateTime desc;

strDT = DateTimeUtil::toStr(table.UtcDateTime);

    return System.DateTime::Parse(strDT);

}

这里,Reinhard同样选择了ReinhardTest_Table表中的最后一条记录,将其返回给DotNet,也就是2015-05-05 05:05:05那条。

接着,在VS中,Reinhard调用这七个服务契约,看看他们返回给DotNet的Date
Time:

Reinhard发现,只有GetUtcDateTime2和GetUtcDateTime3这个两个服务契约,给DotNet返回了正确的DateTime。

结论:正确的日期获取方式是GetUtcDateTime2和GetUtcDateTime3。

4、总结

下面,Reinhard对通过AIF服务契约,进行DateTime存储、获取、比较的正确方式,进行总结。

4.1、DateTime存储

[SysEntryPointAttribute]

public void saveUtcDateTime( utcDateTime _utcDateTime)

{

ReinhardTest_Table table;

table.UtcDateTime=_utcDateTime;

table.insert();

}

4.2、DateTime对比

[SysEntryPointAttribute]

public boolean compareUtcDateTime( utcDateTime _utcDateTime)

{

ReinhardTest_Table table;

    select table order by table.UtcDateTime desc;

    return table.UtcDateTime==_utcDateTime;

}

4.3、DateTime获取

[SysEntryPointAttribute]

public utcDateTime getUtcDateTime2()

{

ReinhardTest_Table table;

    select table order by table.UtcDateTime desc;

    return DateTimeUtil::applyTimeZoneOffset(table.UtcDateTime ,DateTimeUtil::getUserPreferredTimeZone());

}

[SysEntryPointAttribute]

public utcDateTime getUtcDateTime3()

{

ReinhardTest_Table table;

    str strDT;

    select table order by table.UtcDateTime desc;

    return DateTimeUtil::applyTimeZoneOffset(table.UtcDateTime ,DateTimeUtil::getCompanyTimeZone());

}

这次,Reinhard就分享到这里。如果同学们遇到什么问题,也欢迎和Reinhard沟通交流。

Dynamics AX 2012 R2 在AIF服务契约中使用DateTime的更多相关文章

  1. Dynamics AX 2012 R2 安装 AIF IIS上的Web服务

    1.为什么使用IIS上的WEB服务 组件? 如果你要在Dynamics AX Service中使用HTTP Adapter,那么你就要安装IIS上的WEB服务 组件.HTTP Adapter会在IIS ...

  2. Dynamics AX 2012 R2 SSRS报表在VS2010中预览没有数据

    今天,Reinhard 在VS中制作SSRS报表,预览的时候发现显示不出数据. 仔细检查了数据处理环节和临时表里的数据,都发现没有问题. 用同事的账号登陆同样的开发环境,发现他的账号可以在VS中预览到 ...

  3. Dynamics AX 2012 R2 安装Reporting Services 扩展

    今天Reinhard在VS中部署SSRS报表时,接到以下错误: 部署因错误而被取消.在报表服务器上,验证:-SQL Server Reporting Services 服务是否正在运行. 接着,Rei ...

  4. Dynamics AX 2012 R2 如何处理运行时间较长的报表

    当处理的数据量较多,逻辑比较复杂时,报表可能会超时.为了解决这个问题,Reinhard一直使用SrsReportDataProviderPreProcess来做预处理报表.它会在调用SSRS前,在AX ...

  5. Dynamics AX 2012 R2 电子邮件广播错误 0x80040213

    Dynamics AX 2012 R2 电子邮件广播错误 0x80040213 今天Reinhard在新环境做邮件广播测试时,发现无法发送邮件,并报以下错误: 类"CDO.Message&q ...

  6. Dynamics AX 2012 R2 安装额外的AOS

    众所周知,AX系统分为三层:Client,Application Server,Database Server. 我们添加额外的Application Server主要是出于以下两个原因: 使用多台服 ...

  7. Dynamics AX 2012 R2 AIF 内部异常

        今天,Reinhard发现某个入站端口,突然一直报错: The server was unable to process the request due to an internal erro ...

  8. Dynamics AX 2012 R2 Service Middle Tier WCF WCF转发

    参考了蒋金楠老师08年的文章.好吧,那时候我才大二.大三,大神果然是大神. http://www.cnblogs.com/artech/archive/2008/09/01/1280939.html ...

  9. Dynamics AX 2012 R2 业务系列-销售业务流程

    在博文Dynamics AX R2 业务系列中,Reinhard对这个系列做了一个规划,下面我们就按照规划开始说业务吧. 1.销售的主要职责 其实这里说的职责主要是针对销售文员,并非整天外面满世界跑业 ...

随机推荐

  1. NVelocity+Bootstrap tab控件 异常之

    异常信息:Encountered "tings" at line 54, column 55.Was expecting one of:   "(" ...   ...

  2. 测试常用SQL注入语句大全

    转载自Cracer,标题:<渗透常用SQL注入语句大全>,链接http://www.xxxx.com/?p=2226 1.判断有无注入点 整形参数判断 1.直接加' 2.and 1=1 3 ...

  3. bootstrap 不兼容ie8 的问题

    官方推荐的脚手架中,其实已经包含着解决方案:html5shiv.min.js .Respond.min.js 但由于respond.js  使用 file:// 协议,IE8 是无法调起本地文件的   ...

  4. NYOJ背包问题

    #include <stdio.h> struct group{ int value; int weight; }; void Sort(group bag[],int num) { in ...

  5. 将gridFS中的图片文件写入硬盘

    开启用户验证下的gridfs 连接使用,在执行脚本前可以在python shell中 from pymongo import Connectionfrom gridfs import *con = C ...

  6. CMS .NET 程序框架 从2.0/3.5升级到4.0 版本后 需要调整的地方

    问题一: document.forms1.action 不可使用 需要修改程 document.forms[0] .NET 程序框架 从2.0/3.5升级到4.0 版本后,document.forms ...

  7. ARM的一些基本概念

    MPU介绍: mpu是一个芯片,重力加速器(加速度)和陀螺仪(角速度) iic总线.在板上有iic控制器 连接着 最多128个外设,每个外设有地址,可以通信. 寄存器: cpu中的寄存器是为了加快运算 ...

  8. 【iCore3 双核心板】例程十二:通用定时器实验——定时点亮LED

    实验指导书及代码包下载: http://pan.baidu.com/s/1kTWAAJ9 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  9. JQuery-事件(部分)

    /* 1. bind跟on是类似的方法,下面示例可相互替换 $('#click1').on('click',toYellow); // click绑定toYellow方法 $('#click1').o ...

  10. 生成器(generator)内部解析

    #http://kb.cnblogs.com/page/87128/(未看完)