Dynamics AX 2012 R2 在AIF服务契约中使用DateTime

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的更多相关文章
- Dynamics AX 2012 R2 安装 AIF IIS上的Web服务
		1.为什么使用IIS上的WEB服务 组件? 如果你要在Dynamics AX Service中使用HTTP Adapter,那么你就要安装IIS上的WEB服务 组件.HTTP Adapter会在IIS ... 
- Dynamics AX 2012 R2 SSRS报表在VS2010中预览没有数据
		今天,Reinhard 在VS中制作SSRS报表,预览的时候发现显示不出数据. 仔细检查了数据处理环节和临时表里的数据,都发现没有问题. 用同事的账号登陆同样的开发环境,发现他的账号可以在VS中预览到 ... 
- Dynamics AX 2012 R2 安装Reporting Services 扩展
		今天Reinhard在VS中部署SSRS报表时,接到以下错误: 部署因错误而被取消.在报表服务器上,验证:-SQL Server Reporting Services 服务是否正在运行. 接着,Rei ... 
- Dynamics AX 2012 R2 如何处理运行时间较长的报表
		当处理的数据量较多,逻辑比较复杂时,报表可能会超时.为了解决这个问题,Reinhard一直使用SrsReportDataProviderPreProcess来做预处理报表.它会在调用SSRS前,在AX ... 
- Dynamics AX 2012 R2 电子邮件广播错误 0x80040213
		Dynamics AX 2012 R2 电子邮件广播错误 0x80040213 今天Reinhard在新环境做邮件广播测试时,发现无法发送邮件,并报以下错误: 类"CDO.Message&q ... 
- Dynamics AX 2012 R2 安装额外的AOS
		众所周知,AX系统分为三层:Client,Application Server,Database Server. 我们添加额外的Application Server主要是出于以下两个原因: 使用多台服 ... 
- Dynamics AX 2012 R2 AIF 内部异常
		今天,Reinhard发现某个入站端口,突然一直报错: The server was unable to process the request due to an internal erro ... 
- Dynamics AX 2012 R2 Service Middle Tier WCF WCF转发
		参考了蒋金楠老师08年的文章.好吧,那时候我才大二.大三,大神果然是大神. http://www.cnblogs.com/artech/archive/2008/09/01/1280939.html ... 
- Dynamics AX 2012 R2 业务系列-销售业务流程
		在博文Dynamics AX R2 业务系列中,Reinhard对这个系列做了一个规划,下面我们就按照规划开始说业务吧. 1.销售的主要职责 其实这里说的职责主要是针对销售文员,并非整天外面满世界跑业 ... 
随机推荐
- 在mapreduce中做分布式缓存的问题
			一.问题描述: 主要解决一个问题,就是两个表做join,两个表都够大,单个表都无法装入内存. 怎么做呢?思路就是对做join的字段做排序两个表都排序,然后针对一个表a逐行读取,希望能够在内存中加载到另 ... 
- Linux_文件查看
			文件查看 直接查看内容:cat , tac , nl 翻页查看:more , less 指定获取内容:head , tail 查看非纯文字文档:od 文件时间更新与新建:touch cat: 从首行开 ... 
- 制作本地 odoo deb包安装镜像
			[本来这不是个事,可是在阿里云部署的时候,这个网速真是让我无语,本来10分钟就能解决的事,得俩三个小时,太没效率了!] 原文转自 http://www.cnblogs.com/xwdreamer/p/ ... 
- childNodes 和children
			childNodes 兼容性不是很好,一般用children 元素.childNodes : 只读 属性 子节点列表集合标准下:包含了空白换行和元素类型的节点,也会包含非法嵌套的子节点非标准下:只包含 ... 
- 理解Oracle TM和TX锁
			在Oracle中有很多锁,通过v$lock_type视图可以查看Oracle中所有类型的锁,在本篇文章中我们熟悉一下TM和TX锁的类型 SQL> select * from v$lock_typ ... 
- CentOS下IP的配置
			1.打开命令窗口,切换成root账户:su - root 2.进入目录:/etc/sysconfig/network-scripts,打开文件vi ifcfg-eth0 3.修改参数: ## 名称DE ... 
- IOS第七天(3:UiTableView 模型和数据的分组的显示)
			*************UiTableView模型和数据的分组的显示 #import "HMViewController.h" #import "HMHero.h&qu ... 
- LoadRunner,一个简单的例子
			一.录制脚本,这个就不说了,但是可以说说录完一个简单的脚本之后可以做的一些后续工作 1.设置事务的开始跟结束点(参考他人的http://www.cnblogs.com/fnng/archive/201 ... 
- php array_udiff_uassoc比较数组的键值与值
			php array_udiff_uassoc 用于带索引检查计算数组的差集,用回调函数比较数据和索引.本文章通过实例向大家介绍array_udiff_uassoc函数的使用方法.需要的码农可以参考一下 ... 
- py操作mysql
			1.操作mysql的标准流程 import pymysql conn = pymysql.connect(host = "127.0.0.1", port = 3306,user ... 
