Hashtable serialization

Hi, I would like to serialize Hashtable, that is filled with instances of custom made class. Below is an example. // custom made class implementation [DataContract] public class Song {   [DataMember]   public int ID;   [DataMember]   public string Artist;   [DataMember]   public string Song;     public Song() {}     public Song(int ID, string Artist, string Song)   {     this.ID = ID;     this.Artist = Artist;     this.Song = Song;   }    } // serivce implementation [ServiceOperation] Hashtable MediaCatalogue() {   Hashtable HT = new Hashtable();   HT.Add(new Song(1,"Artist","Song"),"1");   HT.Add(new Song(2,"Artist","Song"),"2");    return (HT); } I have successfully hosted service on IIS, but on client side i get deserialization problem. Deserialization is successfull, when Hashtable is filled with string instances, like HT.Add("1","Hello"); HT.Add("2","World"); Thank you. Best regards, Saso


Answer this question

Hashtable serialization

  • gansfield

    Hi, I would like to serialize Hashtable, that is
    filled with instances of custom made class.
    Below is an example.

    // custom made class implementation

    [DataContract]
    public class Song
    {
      [DataMember]
      public int ID;
      [DataMember]
      public string Artist;
      [DataMember]
      public string Song;
     
      public Song() {}
     
      public Song(int ID, string Artist, string Song)
      {
        this.ID = ID;
        this.Artist = Artist;
        this.Song = Song;
      }
    }

    // serivce implementation

    [ServiceOperation]
    Hashtable MediaCatalogue()
    {
      Hashtable HT = new Hashtable();
      HT.Add(new Song(1,"Artist","Song"),"1");
      HT.Add(new Song(2,"Artist","Song"),"2");
      return (HT);
    }

    I have successfully hosted service on IIS, but on client
    side i get deserialization problem. Deserialization is successfull,
    when Hashtable is filled with string instances,
    like HT.Add("1","Hello");
    HT.Add("2","World");

    Thank you.

    Best regards, Saso


  • FrankV

    Saso:

    Use the KnownTypeAttribute to get the XMLFormatter to deserialize the wire goo into your known .NET type.

    This is equivalent to the XMLInclude of the XMLSerializer of .NET 1.*

    HTH



  • Joris Wit

    If you are using Feb CTP please use ServiceKnownType instead of KnownType.

    I hope this works.



  • Ibai Peña

    Thank you for reply.

    I already tryed with KnownTypeAttribute. I did as follows:

    [ServiceOperation]
    [KnownType(typeof(Song))]
    Hashtable MediaCatalogue()
    {
      Hashtable HT = new Hashtable();
      HT.Add(new Song(1,"Artist","Song"),"1");
      HT.Add(new Song(2,"Artist","Song"),"2");
      return (HT);
    }

    Service compiled without errors, but with KnownType attribute
    tool "svcutil" fails. If i did something wrong, could you please
    make up my example in first post

    Thank you.

    Kind regards,
    Saso

  • Kelvin Hoover

    What is the error message you are getting from the SVCUTIL tool



  • Hashtable serialization