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. java输入一个字符串,打印出该字符串中字符的所有排列,随机打乱排序

    import java.util.ArrayList;import java.util.Collections;import java.util.List; public class Test7{   ...

  2. jquery过滤器

    <html> <head> <meta charset="UTF-8"> <title>Document</title> ...

  3. 本地数据库(SQL Server)远程连接服务器端服务器

    本地数据库(SQL Server 2012) 连接外网服务器的数据库,外网的服务器端需要做如下配置: 1. 首先是要打开 数据的配置管理工具 2. 配置相关的客户端协议,开启TCP/IP 3. 数据库 ...

  4. sqlmap 1.0.21 tamper 总结

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. IOS网络第二天 - 02-异步HTTP请求block回调 解析

    ************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interf ...

  6. TCP/IP协议分层

    TCP/IP协议从上而下,层层包装: (1)应用层:HTTP (2)传输层:TCP和UDP (3)网络层(网际互联层):IP (4)数据连接层(网络接入层):为IP模块发送和接收IP数据报. (5)硬 ...

  7. Characteristics of Some CISCs, RISCs, and Superscalar Processors

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Although RISC archite ...

  8. A B-tree index can be used for column comparisons in expressions that use the =, >, >=, <, <=, or BETWEEN operators.

    http://dev.mysql.com/doc/refman/5.7/en/index-btree-hash.html MySQL 5.7 Reference Manual  /  ...  /   ...

  9. 【axc】简单的线性动画绘制

    在一个View上绘制一条直线  然后做出相应的动画效果  可以这样封装三个方法: /** *  划线工具 * *  @param lineArray   线段的点数组 NSValue 类型  默认第一 ...

  10. tomcat服务重启linux

    1杀掉tomcat 进程  用ssh登陆到服务器 lsof -i:8080         //找到端口 ps -ef|grep tomcat kill -9 端口 2找到tomcat目下的start ...