【问题标题】:DateTime type value disappear when sending request to WCF server向 WCF 服务器发送请求时 DateTime 类型值消失
【发布时间】:2012-02-27 18:41:29
【问题描述】:

我有一个向 WCF 服务发送请求的 WCF 客户端。客户端和服务器都在本地运行。请求包含一个 DateTime 类型的值,该值有一个值(例如 DateTime.Now)。 WCF 客户端代理发送请求。但是,当我在请求发送到服务器之前使用 fiddler 捕获请求时,所有 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; }
    }

有什么想法吗?

【问题讨论】:

    标签: c# wcf web-services


    【解决方案1】:

    我无法使用您提供的代码重现该问题:

    [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 有一个很好的comparison

    【讨论】:

      【解决方案2】:

      我解决了这个问题:

      ...
      
          /// <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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-04
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        相关资源
        最近更新 更多