2012-02-27 120 views
0

我有一個WCF客戶端,它向WCF服務發送請求。客戶端和服務器都在本地運行。請求由DateTime類型值組成,該值具有一個值(例如DateTime.Now)。 WCF客戶端代理髮送請求。但是,在將請求發送到服務器之前,使用fiddler捕獲請求時,DateTime類型的所有值都會消失。發送請求到WCF服務器時,DateTime類型值消失

更新:

我手動嘗試都DataContractSerializer的和的XmlSerializer到串行器WCF類(如下所示),其結果是,XmlSerializer的省略了日期時間值(即,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; } 
    } 

任何想法?

回答

0

我無法使用您所提供的代碼來重現問題:

[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中覆蓋客戶端或服務器端的名稱。

除了你已經說過你已經選擇通過DataContractSerializer使用XmlSerializer,但它不清楚爲什麼。 DataContractSerializer在默認情況下使用,比XmlSerializer更具性能。如果您只是在兩個.net應用程序之間進行瀏覽,則還可以查看NetDataContractSerializer。 Dan Rigsby在提到的三個序列器中有一個很好的comparison

0

我解決了這個問題,所以:

... 

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