TJP
TJP

Reputation: 11

WCF Data Contract Data Member with Dictionary<string, object>

I have a data contract that looks something like this:

    [DataContract(Name = "MyResult", Namespace = "MyNamespace")]
    public class MyResult
    {
        [DataMember(Name = "MyValues", Order = 3)]
        public Dictionary<string, object> MyValues { get; set; }
    }

When I populate the object in the Dictionary with simple types like string, int etc.. there is no problem. If I put more complex objects in there such as List (of strings), I get the following error:

An error occurred while receiving the HTTP response to --http://localhost:8081/externalwsapi. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

This error doesn't make sense, I am of course using Http binding. I believe I have a serialization error, but how to make it work?

Upvotes: 1

Views: 6113

Answers (2)

Jack
Jack

Reputation: 715

Because you use object as the type in your collection, WCF serializer can't know what type it can be, so it can't serialize the collection.

You can use KnownTypeAttribue to indicate which kinds of object may be stored in the collection.

Upvotes: 1

Klinger
Klinger

Reputation: 4970

I believe I have dealt with this issue by changing the signature from object to byte[] and doing the serialization myself. I think I used JSON.NET for that. It's been a while now.

Then, on the caller side, I deserialized the payload to what I wanted.

Upvotes: 1

Related Questions