2012-03-29 55 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