0
votes

I've run into an interesting situation in my WCF webservice where I am experiencing different behaviour between my locally deployed application running in IIS and my deployed Web Role running on Azure.

I have a class defined as such

[DataContract]
public class DefaultClassView : BaseView
{
    [DataMember]
    public Guid ClassID { get; set; }

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

    [DataMember]
    public Guid SchoolID { get; set; }

    [DataMember]
    public string SchoolName { get; set; }

    [DataMember]
    public IEnumerable<Guid> YearLevelIDs { get; set; }

    [DataMember]
    public IEnumerable<string> YearLevelNameList { get; set; }

    [DataMember]
    public IEnumerable<Guid> SubjectIDs { get; set; }

    [DataMember]
    public IEnumerable<string> SubjectNameList { get; set; }
}

The class is then populated using the following code

public static DefaultClassView ConvertToView(this Class entity)
{
    return new DefaultClassView()
    {
        ClassID = entity.ClassID,
        Name = entity.Name,
        YearLevelIDs = entity.Students.Select(o => o.YearLevelID).Distinct(),
        YearLevelNameList = entity.Students.Select(o => o.YearLevel.Name).Distinct(),
        SchoolID = entity.SchoolID,
        SchoolName = entity.School.Name,
        SubjectIDs = entity.Subjects.Select(o => o.SubjectID),
        SubjectNameList = entity.Subjects.Select(o => o.Name)
    };
}

This code has worked perfectly (while deployed in my local IIS instance) until I deployed it on Azure, at which point I began receiving the following error when the service was activated.

Type System.Linq.Enumerable+WhereSelectEnumerableIterator`2[Model.Subject,System.Guid]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. 

Upon removal of the LINQ queries I was able to successfully fix this issue but what I would like to understand is why this behaviour is so different between the two platforms and if infact this behaviour is by design, I'd also like to know if there are any other similar "Gotchas" that I need to be aware of related to deploying WCF services on Azure?

1

1 Answers

0
votes

After every LINQ query, call ToList(), First(), or FirstOrDefault() so you are not serializing LINQ types, but the types intended.