CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    357

    Serialising Custom Objects ASP Webservice

    Hey,

    I asked a similar question a while back but never got a response...

    Ive got a problem where i have a web method (like shown below)

    Code:
    // Class Example
    public CustomClass
    {
       protected int Var1 = 10;
       protected String Var2 = "Example";
    
       public int IntVar { get { return Var1; } }
       public String StringVar { get { return Var2; } }
    }
    
    // Web Method Example
    [WebMethod]
    CustomClass GetClass()
    { return new CustomClass(); }
    
    // Example Output
    <CustomClass>
    <IntVar>10</IntVar>
    </CustomClass>
    Now thats a simple example, you call the method and get back a custom class, now if i view that method in the browser i see the object in XML but its missing alot of the content of the class, it only seems to display the int values and none of the Strings, even though each member has a public property assigned.

    Is there some sort of attribute that needs to be applied to have control over how the XML serialises the object for returning?

    Any advice would be great! (Again just want to point out that this is a VERY simple example of what my problem is, not point pasting reams of code in here, when that should be enough)
    Last edited by Grofit; January 27th, 2009 at 12:32 PM.

  2. #2
    Join Date
    Nov 2006
    Posts
    357

    Re: Serialising Custom Objects ASP Webservice

    I was looking and i can apparently use [Serializable()] on classes, but then i dont think they can be inherited... i also tried testing by assing [XmlElement("MyVar")], but then the webservice just bombs saying:

    Could not load file or assembly 'System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.

    So again, ANY help or advice is appreciated, there seems to be LOADS of information about making a simple webservice to tell you the color of the sky or the current time, but there isnt much information i found on using custom classes and polymorphism in web services (although i covered polymorphism in a previous thread here)...

  3. #3
    Join Date
    Nov 2006
    Posts
    357

    Re: Serialising Custom Objects ASP Webservice

    a bump plea, incase anyone has anything to offer... i dont think im trying to do any voodoo magic so im supprised no one has been able to give me any tips

  4. #4
    Join Date
    Nov 2006
    Posts
    357

    Re: Serialising Custom Objects ASP Webservice

    Just incase anyone is having same problem its down to the properties not having setters, i think im going to have to make my own serialize/unserialize methods and just pass string data around...

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Serialising Custom Objects ASP Webservice

    What you could do, is the next:

    Give the properties in your CustomClass both getters and setters. Let the webmethod return a interface of this CustomClass with only getters

  6. #6
    Join Date
    Nov 2006
    Posts
    357

    Re: Serialising Custom Objects ASP Webservice

    I see where you are coming from but some of them need setters, whereas others dont, like i have a lookup ID value that can change, but the associated String name is only readonly. I also extend the base class so it may have additional members not present in the base class, i guess i could always extend the interface to reflect it but it seems to be getting a bit out of hand there...

    What i really wanted was 1 library that contained all my class definitions, that could be shared between applications and websites, and a web service that would use the library and pass around instances of the classes based off data provided from the database.

    I must admit im not that experienced with webservices and ASP, so i presumed that it would just serialize the class into some generic structure so anything on the other side could just unserialize it.

    As i said in the other post i can always use custom serialize/unserialize methods to pass the class through the webservice and reform it on the other side.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured