[英]DateTime type value disappear when sending request to WCF server
我有一个WCF客户端,它向WCF服务发送请求。 客户端和服务器均在本地运行。 该请求包含一个DateTime类型的值,该值具有一个值(例如DateTime.Now)。 WCF客户端代理发送请求。 但是,当我使用提琴手在将请求发送到服务器之前捕获请求时,所有DateTime类型的值都消失了。
更新:
我尝试将DataContractSerializer和XmlSerializer都手动序列化为WCF类(如下所示),结果是XmlSerializer省略了DateTime值(即DateTime值消失了),DataContractSerializer保留了该值。
因为WCF服务器正在使用XmlSerializer,并且由于客户端代理类数量很多,所以理想情况下,客户端应使用XmlSerializer。
WCF客户端:
WCF代理类:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
//[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:core_e-nbp-v1.0")]
public partial class ClaimApplication : object, System.ComponentModel.INotifyPropertyChanged {
private System.DateTime hBEffectiveDateField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date", Order = 3)]
public System.DateTime HBEffectiveDate {
get {
return this.hBEffectiveDateField;
}
set {
this.hBEffectiveDateField = value;
this.RaisePropertyChanged("HBEffectiveDate");
}
}
}
//Assign a random DateTime value
claimApplication.HBEffectiveDate = DateTime.Now.ToUniversalTime();
//manully serialize to check the DateTime using XmlSerializer
XmlSerializer s = new XmlSerializer(typeof(ClaimApplication));
StreamWriter sw = new StreamWriter(@"D:\xmlsamples\XmlSerializer.xml");
s.Serialize(sw,claimApplication);
sw.Dispose();
//manully serialize to check the DateTime using DataContractSerializer
DataContractSerializer dc = new DataContractSerializer(typeof(ClaimApplication));
FileStream fs = new FileStream(@"D:\xmlsamples\DataContractSerializer.xml", FileMode.CreateNew);
dc.WriteObject(fs,claimApplication);
fs.Dispose();
WCF服务器:
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime EffectiveDate
{
get { return this.EffectiveDateField; }
set { this.EffectiveDateField = value; }
}
任何想法?
我解决了这个问题,所以:
...
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date", Order = 1)]
public System.DateTime BirthDate
{
get
{
return this.birthDateField;
}
set
{
this.birthDateField = value;
this.RaisePropertyChanged("BirthDate");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool BirthDateSpecified {
get {
return this.birthDateFieldSpecified;
}
set {
this.birthDateFieldSpecified = value;
this.RaisePropertyChanged("BirthDateSpecified");
}
}
...
和
contact.BirthDate = DateTime.Now;
contact.BirthDateSpecified = true; //<-- See here
我无法使用您提供的代码重现该问题:
[TestFixture]
public class When_Scenario
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
//[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:core_e-nbp-v1.0")]
public partial class ClaimApplication : object, System.ComponentModel.INotifyPropertyChanged
{
private System.DateTime hBEffectiveDateField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date", Order = 3)]
public System.DateTime HBEffectiveDate
{
get
{
return this.hBEffectiveDateField;
}
set
{
this.hBEffectiveDateField = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
[Test]
public void Should_Assertion()
{
ClaimApplication claimApplication = new ClaimApplication();
claimApplication.HBEffectiveDate = DateTime.Now.ToUniversalTime();
XmlSerializer s = new XmlSerializer(typeof(ClaimApplication));
s.Serialize(Console.Out, claimApplication);
}
}
因为您正在使用XmlSerialization,所以除非您要序列化复杂类型,更改名称空间或要在Xml中使用的属性的名称,否则不必装饰要序列化的成员。 我可以看到有一些生成的代码,但尚不清楚是什么生成的以及生成的原因。 我唯一能看到的是,服务器端和客户端的实体之间存在名称差异。 为了使它起作用,您将需要在XmlElementAttribute的客户端或服务器端覆盖该名称。
除此之外,您还说过您选择了使用XmlSerializer而不是DataContractSerializer,但尚不清楚原因。 默认情况下,DataContractSerializer是开箱即用的,并且比XmlSerializer的性能更高。 如果仅在两个.net应用程序之间切换,则还可以查看NetDataContractSerializer。 Dan Rigsby对上面提到的三个串行器进行了很好的比较 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.