3

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)
    {
        ...
    }
}
Jean-Philippe Leclerc
  • 6,713
  • 5
  • 43
  • 66
  • What is the layout of the `CustomStruct`? – linuxuser27 Jun 16 '11 at 15:25
  • Can you post the code for your `CustomStruct` ? Is it marked 'Serializable` ? – Bala R Jun 16 '11 at 15:26
  • Could you also supply the generated CustomStruct from the proxy (assuming you used Add Service Reference) - you'll find it in the Reference.cs file if you use Show all files on the service reference - also is CustomStruct in a namespace? – Richard Blewett Jun 16 '11 at 15:33
  • I'm not quite sure of what is Reference.cs, but yes CustomStruct is in a namespace – Jean-Philippe Leclerc Jun 16 '11 at 15:39
  • I don't know how you managed to get a dictionary of string to string serialized, but in general, anything implementing the IDictionary interface is not currently serializable. You'd be looking at implementing your own wrapper. Check out [the MSDN article](http://msdn.microsoft.com/en-us/library/ms950721.aspx) (search IDictionary). – lsuarez Jun 16 '11 at 16:11
  • Your struct does not have any public properties marked with the [DataMember] tag – cadrell0 Jun 16 '11 at 16:44
  • Dictionary serializes just fine – Richard Blewett Jun 16 '11 at 16:45
  • @cadrell0 the DataContractSerializer will serialize private state as long as it is attributed – Richard Blewett Jun 16 '11 at 16:46
  • Did you create the client using Add Service Reference? If so, in the client project, select the service reference and then press the show all files button. If you open the Reference.svcmap node you will see reference.cs. Can you show us that definition and also tell us what the namespace is for CustomStruct. I have a sample with your scenario working – Richard Blewett Jun 16 '11 at 16:54

2 Answers2

1

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

Community
  • 1
  • 1
Russel Yang
  • 2,633
  • 3
  • 21
  • 18
1

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

Richard Blewett
  • 6,089
  • 1
  • 18
  • 23