简体   繁体   English

从.net和WCF消耗Java Webservice时间戳

[英]Consuming Java Webservice TimeStamp from .net and WCF

We are trying to talk to a (possible)Java WebService from .NET 3.5 using WCF. 我们正在尝试使用WCF与.NET 3.5中的(可能)Java WebService进行通信。 The WebService defines a Timestamp object which seems to be a datetime without the decimal milliseconds. WebService定义了一个Timestamp对象,该对象似乎是一个不带小数毫秒的日期时间。

WCF decided .NET would use a datatime as the object backing in the proxy class. WCF决定.NET将使用datatime作为代理类中的对象支持。 When sending objects with the timestamp to the Java WebServer the datetime is serialized and includes the miliseconds. 当将带有时间戳的对象发送到Java WebServer时,日期时间将被序列化并包含毫秒。 This results in a Fault. 这会导致故障。

How do we a) Cause .NET to select a custom class we implement (So we can format the message) or b) Cause WCF to generate the datetime based on the XSD files included with the WSDL? 我们如何a)使.NET选择我们实现的自定义类(以便我们可以格式化消息),或b)使WCF根据WSDL随附的XSD文件生成日期时间?

Issue Definition: Timestamp needs to look like: 2010-01-01T01:01:01-07:00 问题定义:时间戳需要看起来像:2010-01-01T01:01:01-07:00

Yet .NET generates a timestamp with decimal seconds like: 2010-01-01T01:01:01.1234-07:00 但是.NET会生成带有小数秒的时间戳,例如:2010-01-01T01:01:01.1234-07:00

您是否可以修改代码以将其作为字符串传递并以.ToString()方法设置其格式,如以下代码片段所示?

SomeTime.ToString("yyyy-MM-ddThh:mm:ss")

By "WCF decided .NET would use a datetime", I get the feeling you used visual studio to add a reference to the web service. 通过“ WCF决定.NET将使用日期时间”,我感到您使用Visual Studio添加了对Web服务的引用。

Instead I'd recommend using SvcUtil to generate a proxy, then add that to your project. 相反,我建议使用SvcUtil生成代理,然后将其添加到您的项目中。 This way you can edit the proxy directly and make it behave how you need (for example, serializing a string rather than a datetime, or formatting it differently). 这样,您可以直接编辑代理并使其表现出所需的状态(例如,序列化字符串而不是日期时间,或以其他方式设置其格式)。

Any time the service contract changes, you need to create a new proxy and merge your changes in, but this is normally a small price to pay for the additional control. 每当服务合同更改时,您都需要创建一个新的代理并合并您的更改,但这通常是为支付额外的控制权而付出的很小的代价。

I figured out a few ways to handle this problem. 我想出了几种方法来解决这个问题。 The more complicated methods involve hooking of a custom MessageFormatter Endpoint. 更复杂的方法涉及挂钩自定义MessageFormatter端点。

We found a simple way to to this. 我们找到了一种简单的方法。

the Fraction of seconds are only generated if the datetime object has them. 仅当datetime对象具有秒的分数时,才生成秒的分数。

What we did: 我们做了什么:

We created a static on propertychange event handler that uses reflection to detect datetime datatypes. 我们创建了一个静态的on propertychange事件处理程序,该处理程序使用反射来检测日期时间数据类型。 When found we recreate the datetime without the fractions of seconds. 找到后,我们将重新创建日期时间,而不用秒的分数。 In our case we didn't care about seconds at all. 在我们的情况下,我们根本不关心秒。 We wire the event up in a partial class constructor. 我们将事件连接到局部类构造函数中。 Thats it. 而已。

Of course 当然

public static class DateTimeSecondCatcher
{
    PropertyInfo dateTimePropertyInfo = sender.GetType().GetProperty(e.PropertyName);
        if ((dateTimePropertyInfo != null) && (dateTimePropertyInfo.PropertyType == typeof(DateTime)))
        {

            DateTime dteValue = (DateTime)dateTimePropertyInfo.GetValue(sender, null);
            if (dteValue.Millisecond > 0)
            {
                dateTimePropertyInfo.SetValue(sender, new DateTime(dteValue.Year,dteValue.Month,dteValue.Day, dteValue.Hour,dteValue.Minute,dteValue.Second,0,dteValue.Kind), null);
            }
        }

}


// This code goes in the partial class constructor
this.PropertyChanged += new PropertyChangedEventHandler(DateTimeSecondCatcher.OnPropertyChanged);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM