0
votes

I have a WCF service, with a method UpdateProperty that take a PropertyValue in parameter :

void UpdateProperty(PropertyValue propertyValue)

[DataContract]
public class PropertyValue
{
    [DataMember]
    public string Property { get; private set; }

    [DataMember]
    public object Value { get; private set; }
}

I'm trying to call the method with a KeyValuePair as Value, and I'm getting this error :

"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:propertyValue. The InnerException message was 'Error in line 1 position 544. Element 'http://schemas.datacontract.org/2004/07/MyProject.DataContracts:Value' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/System.Collections.Generic:KeyValuePairOfanyTypeanyType'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'KeyValuePairOfanyTypeanyType' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to the serializer.'. Please see InnerException for more details."

I tried to add a KnownType on my class but I still get the error :

[KnownType(typeof(KeyValuePair<object, object>))]

Any idea why ? I'm using other KeyValuePair in other method (as parameter) without problem...

In my SOAP body, I have :

    <d4p1:Value xmlns:d7p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" i:type="d7p1:KeyValuePairOfanyTypeanyType">
      <d7p1:key>test</d7p1:key>
      <d7p1:value>test</d7p1:value>
    </d4p1:Value>
1
What is the actual type of KeyValuePair<,> that you are serializing over the wire? Is it really a KeyValuePair<object, object> or is it, say, a KeyValuePair<string, string>?dbc
I tried both, but what I really need for my use case is a KeyValuePair<DateTime, DateTime>user3544117
It seems that he didn't know the Type that you use in your Property Value. Mark your class PropertyValue with [KnownType( typeof(MyType) )], MyType is the Type that you give him in the Value Property.Rabban
1) Can you show how you put the [KnownType(typeof(KeyValuePair<object, object>))] attribute on PropertyValue? Did you modify the contract on both the client and server sides to include this known type?dbc
I simply added it to the class on the server. Then I updated the service reference on the client. In the generated proxy, I have the new KnownType but it still doesn't work : [System.Runtime.Serialization.KnownTypeAttribute(typeof(System.Collections.Generic.KeyValuePair<object, object>))] public partial class PropertyValue : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {user3544117

1 Answers

0
votes

i guess i know what you want. try this object as your input:

    [Serializable]
public class WebServiceInputGetDataParams : ISerializable
{
    public Dictionary<string, object> Entries
    {
        get { return entries; }
    }


    private Dictionary<string, object> entries;
    public WebServiceInputGetDataParams()
    {
        entries = new Dictionary<string, object>();
    }
    protected WebServiceInputGetDataParams(SerializationInfo info, StreamingContext context)
    {
        entries = new Dictionary<string, object>();
        foreach (var entry in info)
        {
            entries.Add(entry.Name, entry.Value);
        }
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (var entry in entries)
        {
            info.AddValue(entry.Key, entry.Value);
        }
    }
}