I want to obtain avaible data contract types at run-time, that's why I'm using ServiceKnownType
attribute:
[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]
public interface IService1
{
[OperationContract]
object GetData(bool list);
}
public static class KnownTypesProvider
{
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
return new[] { typeof(CompositeType) };
}
}
I have no problem when returning a CompositeType
from GetData
method, but returning a list (List<CompositeType>
) or an array (CompositeType[]
) causes a serialization error:
public class Service1 : IService1
{
public object GetData(bool list)
{
return list ?
// serialization error
(object)new List<CompositeType>() :
// successfull result
new CompositeType();
}
}
Error is:
There was an error while trying to serialize parameter http://tempuri.org/:GetData. The InnerException message was 'Type 'System.Collections.Generic.List`1[[WcfService2.CompositeType, WcfService2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfCompositeType:http://schemas.datacontract.org/2004/07/WcfService2' is not expected. 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 DataContractSerializer.
I thought WCF supports collection serialization by default. Am I missing something?
How to fix this?