简体   繁体   中英

WCF serialization gotcha - Different behaviour Azure Web Role VS IIS

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?

在每个LINQ查询之后,调用ToList(),First()或FirstOrDefault(),这样您就无需序列化LINQ类型,而可以序列化所需的类型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM