Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
    {
        ...
    }
}
like image 753
Jean-Philippe Leclerc Avatar asked Nov 18 '25 23:11

Jean-Philippe Leclerc


1 Answers

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

like image 144
Richard Blewett Avatar answered Nov 21 '25 11:11

Richard Blewett