Jean-Philippe Leclerc
Jean-Philippe Leclerc

Reputation: 6805

Dictionary<struct,int> not serializing properly

I have a WCF service and I try to send a Dictionary<CustomStruct, int> from my client to my server. My WCF service use a default BasicHttpBinding.

When I send the Dictionary to the server, no error is thrown. But when I try to loop trough my Dictionary, it is empty.

The weird thing is that Dictionary<string, string> actually works ?!

Does someone have an idea of why my Dictionary<CustomStruct, int> is empty after it passes trough the wire and why Dictionary<string, string> works.

[EDIT] Here's how my struct looks like:

[DataContract]
public struct CustomStruct : IEquatable<CustomStruct>
{
    [DataMember]
    private string _prop;

    public string Prop { get { return _prop; } }

    public override int GetHashCode()
    {
        return Prop.GetHashCode();
    }

    public static bool operator ==(CustomStruct left, CustomStruct right)
    {
        ...
    }

    public static bool operator !=(CustomStruct left, CustomStruct right)
    {
        ...
    }

    public override bool Equals(object obj)
    {
        ...
    }
}

Upvotes: 3

Views: 782

Answers (2)

Richard Blewett
Richard Blewett

Reputation: 6109

Here is a working sample that does what you want

http://rocksolidknowledge.blob.core.windows.net/demos/CustomStructSerialization.zip

Unfortunately from the code you have so far provided I can;t see what's wrong - at a guess you have different namespaces on your CustomStruct at client and service and so the serialization isn't working correctly but without seeing the generated code and the custom struct more fully i can't tell

Upvotes: 1

Russel Yang
Russel Yang

Reputation: 2701

Try to see if this FAQ help. WCF service returning an array of dictionary<string, object>

Upvotes: 1

Related Questions