I would like to send a list of Appointments through WCF. My Interface looks like this:

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

If I call my WCF Service I'm always getting the following error:

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.

My Service currently looks like this:

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

解决方案

It's not your service that's at fault, it's the class your passing, Appointment. Start by adding [DataContract] to your class. then [DataMember] to each of the properties you'd like to pass.

For example, if you started with:

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

You can make it serializable by WCF's DataContractSerializer by adding those attributes:

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

    [DataMember]
    public string Name { get; set; }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top