0

I am trying to send a object via WCF as parameter in this method:

[OperationContract]
bool SendProject(Project project);

and I got this if i try to call it from client:

There was an error while trying to serialize parameter project. The InnerException message was 'Type 'System.Collections.ObjectModel.Collection`1[[System.Collections.DictionaryEntry, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfDictionaryEntry:http://schemas.datacontract.org/2004/07/System.Collections' is not expected. Consider using a DataContractResolver 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 DataContractSerializer.'. Please see InnerException for more details.

I search some info and I think that the error is how I serialize a class (a class from Dr. WPF) which is inside "project" class:

#region ISerializable

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
    if (info == null)
    {
        throw new ArgumentNullException("info");
    }
            
    Collection<DictionaryEntry> entries = new Collection<DictionaryEntry>();
    foreach (DictionaryEntry entry in _keyedEntryCollection)
        entries.Add(entry);
    info.AddValue("entries", entries);
}

#endregion ISerializable

The problem is that I dont know where to put the tag "KnownType" or how to serialize correctly this Dictionary to send it as parameter using WCF.

Thanks!

3
  • 1
    The problem is that DictionaryEntry is non-generic and so WCF will not know the contracts for the keys and values. But why are you using a non-generic dictionary from .Net 1.1? Why not use a typed model? WCF is designed to work with typed models. Alternatively, if you can't use a typed model, can you share a minimal reproducible example that reproduces the problem? At the moment you just share a code fragment that don't compile standalone. Commented Aug 21, 2020 at 17:22
  • I change the DictionaryEntry for KeyValuePair<TKey, TValue> but the error just persist but this time with KeyValuePair as parameter. The Dictionary is used multiple times and it has not a type defined, it is <string, ClassA> or <string, classB>. It would be difficult send a minimal reproducible example because the code has complexity, but i think you can reproduce it dowlnoading the code from Dr.WPF link and using it in WCF in a custom class. I will try to simplify the code if this solution doesnt work. Thanks for your help! Commented Aug 24, 2020 at 9:32
  • Since you're just serializing a Collection<DictionaryEntry> (or Collection<KeyValuePair<string, something you haven't shown us>>)maybe you can reproduce this with a simple Dictionary<TKey, TValue> stored inside a root object? Commented Aug 24, 2020 at 19:15

1 Answer 1

1

I have the solution, which is not so clean but is a solution. As dbc said, updating DictionaryEntry to KeyValuePair<TKey,TValue> is a recommended step, but what fixed my problem was set [KnownType(typeof(Collection<KeyValuePair<TypeX, TypeY>>))] in the class where the serialization is.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.