Home

Keebler/Blog

« Fixing Connect360 on the New Xbox Experience
When NOT to start an online store »

C#: Returning an array via webservice with derived classes

Here’s a neat little thing I learned today. I was writing a WebMethod for a WebService I’m working on for ScribbleLive. The return type is an array of Post objects.

[WebMethod]
public Post[] GetPostsSince( int ThreadId, DateTime Since )
{ ... }

But sometimes the array actually has objects of the type Comment, that is a derived class of Post. When I tried tried it out, it threw this exception:

System.InvalidOperationException: There was an error generating the XML document. —> System.InvalidOperationException: The type Comment was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

The problem is that the Serializer doesn’t know about the Comment class until runtime, and it throws a tantrum. Luckily, the fix is just to put a directive in there to make sure it knows that the Comment class may be used.

In my case, I just added the XmlInclude directive right about my Post class declaration like this:

[Serializable()]
[XmlInclude( typeof( Comment ) )]
public class Post
{ ... }

And that did the trick! I hope this is helpful for someone else :)

Tags: C#, programming, webservice

Related Posts

  • Generic tweet: I am returning …

This entry was posted on Thursday, November 20th, 2008 at 11:25 am and is filed under Web Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “C#: Returning an array via webservice with derived classes”

  1. Dominik says:
    February 20, 2009 at 10:29 am

    Thank man!!

    You save my life.

Leave a Reply

Click here to cancel reply.

  • Blog Home

  • Tags

    90210 alessandra ambrosio animals asp.net audrina patridge battlestar galactica bikini bunny bush C# canada canada election ctv ctvglobemedia development Election Entertainment/Celebrity facebook Gordon Ramsay gorilla gossip girl hayden panettiere iphone last.fm liveblog liveblogging mesh08 muchmusic Music obama palin rachel bilson scribblelive shenae grimes subway telemarketers the hills the killers the office toronto ttc toronto tv us election video yeah yeah yeahs

    WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

  • Recent Posts

    • Kenny (vs. Spenny) livebloggin…
    • Trying to convince @idiotbante…
    • I wonder if the CTV “Save Loca…
    • Just finished dinner which I m…
    • I shouldn’t get as excited abo…
  • Categories

    • Election
    • Entertainment/Celebrity
    • Fake News
    • Featured
    • Funny
    • Keebler/Show
    • Music
    • My Releases
    • On-the-Road
    • RedDot
    • Reviews
    • Science/Technology
    • ScribbleLive
    • Security
    • Software
    • Torrent Live Stats
    • Tweets
    • Uncategorized
    • Web Development
  • Blogroll

    • Kitten-Monkey’s Cell
    • :: kattekylling ::
    • Adam Finley
    • blog-j
    • fuzzz.gaulin.ca
    • Idiot Banter
    • mad.greyarea.com
    • ob.blog
    • PollyPrissyPants
    • RedDot CMS Users Google Group
    • ScribbleLive Official Blog
  • My Sites

    • Flickr2Facebook
    • RedDot CMS
    • ScribbleLive
    • Where are my f(acebook)ing friends?
    • WhyYouShould