0
votes

I have a simple object with a propery (I have more properties, simple ints and strings);

    [DataMember]
    public virtual IList<MailAddress> DistributionList { get; set; }

I read this object using NHibernate, resulting in the property beeing of type NHibernate.Collection.Generic.PersistentGenericList.

I get the following error when looking at the WCF log;

There was an error while trying to serialize parameter http://tempuri.org/:StartReportDistributionResult. The InnerException message was

Type 'Miros.Models.MailAddress' with data contract name 'MailAddress:http://schemas.datacontract.org/2004/07/Miros.Models' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.'. Please see InnerException for more details.

Now this should indicate that there is something wrong with serialization. So I added code which test the object through the DataContractSerializer, from this question Has anyone created a DataContract testing tool?. This works fine...

Now if I change the property;

myObject.DistributionList = myObject.DistributionList.ToList();

WCF is happy. What is going on here? How can I find out what is the problem.

1
did you marked MailAddress and its members with DataContract/DataMember too?Ricardo Pontual
Yes I did. And it works through the test serialization and deseralization.Julius
Inner exception? It's always better to simply call ToString on the exception object and paste the entire result into your question. Also, have you tried applying the KnownTypeAttribute to the class with the property? It may be annoying but it might solve your issue.user1228
I will try, but it does not explain why it is happy going from NHibernates collection to List.Julius
I think defining KnownType attribute will solve your problem as suggested by @WillViru

1 Answers

0
votes

It may be due to the fact that the DataMember is of type IList. Try changing it to concrete class List instead.

I found it a bit problematic to use generic interface type data members in WCF contracts especially if the interface member is not initialized properly or it is lazy loaded, I recall having similar serialization issues with WCF serialization using IEnumerable type Data Members.