2012-03-29 96 views
1

我想通過WCF發送Appointment的清單。我的界面看起來是這樣的:通過WCF發送預約清單

[ServiceContract] 
    public interface IServices 
    { 
     [OperationContract] 
     string addAppointments(List<Appointment> appointmentList); 
    } 

如果我把我的WCF服務我總是收到以下錯誤:

Type 'Microsoft.Exchange.WebServices.Data.Appointment' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.

我的服務目前看起來是這樣的:

class Service : IServices 
    { 
     public string addAppointments(List<Appointment> appointmentList) 
     { 
      foreach (Appointment app in appointmentList) 
      { 
       Console.WriteLine(app.Organizer.Name); 
      } 
      return "true"; 
     } 
    } 
+2

它看起來像'Microsoft.Exchange.WebServices.Data。約會「是你從其他地方獲得的類,它不打算序列化。 – 2012-03-29 13:38:45

回答

2

這是不是你的服務有問題,而是你通過的課程,約會。 首先將[DataContract]添加到您的班級。然後將[DataMember]添加到您想要傳遞的每個屬性。

例如,如果你開始:

public class Appointment{ 
    public DateTime Date { get; set; } 
    public string Name { get; set; } 
} 

你可以把它序列化的WCF的DataContractSerializer的通過添加這些屬性:

[DataContract]  
public class Appointment{ 
    [DataMember] 
    public DateTime Date { get; set; } 

    [DataMember] 
    public string Name { get; set; } 
} 
+0

在哪個DLL我可以找到DataContract?我試着用「使用System.Runtime.Serialization」但它對我無效 – andreaspfr 2012-03-29 13:49:36

+1

如果OP在實體上沒有任何[DataContract]屬性,它將採用默認方法並序列化所有公共屬性。根據@Jesse Slicer,似乎該實體不是POCO,不能被序列化。 http://msdn.microsoft.com/en-us/library/ms733127.aspx – StuartLC 2012-03-29 13:58:07