WCF client is receiving a Date value from a Java web service where the date sent to the client in XML is :
2010-05-10+14:00
Try this
DateTime newDate = DateTime.SpecifyKind(oldDate, DateTimeKind.Unspecified);
Coding Best Practices Using DateTime in the .NET Framework
Related links
http://daveonsoftware.blogspot.com/2008/07/wcf-datetime-field-adjusted.html
http://social.msdn.microsoft.com/forums/en-US/wcf/thread/36ae825a-ffc6-4ac3-9981-c82692039d58
Best practices for DateTime serialization in .NET 3.5
Hi my improved and more universal solution
public static class TimeZoneMessageHelper
{
public static Message RemoveTimeZone(this Message message)
{
try
{
using (XmlDictionaryReader messageBodyReader = message.GetReaderAtBodyContents())
{
XmlDocument xmlDocument = new XmlDocument();
Message returnMessage = null;
xmlDocument.Load(messageBodyReader);
RemoveTimeZone(xmlDocument);
StringBuilder stringBuilder = new StringBuilder();
using(XmlWriter xmlWriter = XmlWriter.Create(stringBuilder))
{
xmlDocument.Save(xmlWriter);
}
// do not dispose to early
XmlReader resultMessageBodyReader = XmlReader.Create(new StringReader(stringBuilder.ToString()));
returnMessage = Message.CreateMessage(message.Version, null, resultMessageBodyReader);
returnMessage.Headers.CopyHeadersFrom(message);
returnMessage.Properties.CopyProperties(message.Properties);
return returnMessage;
}
}
}
private static void RemoveTimeZone(XmlNode xmlNode)
{
if (xmlNode.ChildNodes.Count == 0)
{
RemoveTimeZoneCore(xmlNode);
}
else
{
foreach(XmlNode node in xmlNode.ChildNodes)
RemoveTimeZone(node);
}
}
public static void RemoveTimeZoneCore(XmlNode xmlNode)
{
if (xmlNode != null)
{
if (Regex.IsMatch(xmlNode.InnerText, @"^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d\d)?(\+|-)\d\d:\d\d$", RegexOptions.Compiled))
{
xmlNode.InnerText = xmlNode.InnerText.Substring(0, xmlNode.InnerText.Length - 6);
}
}
}
}
public class RemoveTimeZoneClientMessageFormatter : IClientMessageFormatter
{
private readonly IClientMessageFormatter original;
public RemoveTimeZoneClientMessageFormatter(IClientMessageFormatter original)
{
this.original = original;
}
#region Implementation of IClientMessageFormatter
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
return original.SerializeRequest(messageVersion, parameters).RemoveTimeZone();
}
public object DeserializeReply(Message message, object[] parameters)
{
return original.DeserializeReply(message.RemoveTimeZone() ?? message, parameters);
}
#endregion
}
public class RemoveTimeZoneDispatchMessageFormatter : IDispatchMessageFormatter
{
private readonly IDispatchMessageFormatter original;
public RemoveTimeZoneDispatchMessageFormatter(IDispatchMessageFormatter original)
{
this.original = original;
}
#region Implementation of IDispatchMessageFormatter
public void DeserializeRequest(Message message, object[] parameters)
{
original.DeserializeRequest(message.RemoveTimeZone() ?? message, parameters);
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
return original.SerializeReply(messageVersion, parameters, result).RemoveTimeZone();
}
#endregion
}
public class RemoveTimeZoneEndpointBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
operation.Behaviors.Add(new RemoveTimeZoneOperationBehavior());
}
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach(OperationDescription operation in endpoint.Contract.Operations)
{
operation.Behaviors.Add(new RemoveTimeZoneOperationBehavior());
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
public class RemoveTimeZoneOperationBehavior : IOperationBehavior
{
public void AddBindingParameters(OperationDescription description, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
{
proxy.Formatter = new RemoveTimeZoneClientMessageFormatter(proxy.Formatter);
}
public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation operation)
{
operation.Formatter = new RemoveTimeZoneDispatchMessageFormatter(operation.Formatter);
}
public void Validate(OperationDescription description)
{
}
}
public class RemoveTimeZoneExtensionElement : BehaviorExtensionElement
{
public override Type BehaviorType
{
get
{
return typeof(RemoveTimeZoneEndpointBehavior);
}
}
protected override object CreateBehavior()
{
return new RemoveTimeZoneEndpointBehavior();
}
}\