logo
  • Jobs
  • About Me
  • Contact
  • Home
« The Shining, Redux
VS 2005 Web Projects »

Web services and derived classes

Posted October 3rd, 2005 by Matt Berther
[WebMethod]
public MyBase SayHello()
{
    return new MyDerivedClass();
}
 
public class MyBase
{
}
 
public class MyDerivedClass : MyBase
{
}

When doing this in an ASP.NET 2.0 (and I imagine ASP.NET 1.1 functions similarly), I get hit with this exception:

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

This seems to be a major design flaw in the implementation of the Xml Serializer (which was rumoured to be completely revamped in 2.0). It seems strange that I should need to go into my web service and add an XmlInclude attribute every time I add a new derived class.

Can anyone explain this, or any possible workarounds?

3 Comments

This entry was posted on Monday, October 3rd, 2005 at 12:16 pm and is filed under Uncategorized. 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.

David Hervieux
October 3rd, 2005

It’s because of the XSD. The wsdl must contains a description of every return types explained by an xsd, so derived type cannot be resolved.

The only workarround is to create a kind of container that implements IXmlSerializable. It’s must serialize manually every type and add also the type name for the deserilization but I will not be cross plateform.

Hope this help

David

Seth Flowers
June 27th, 2006

I’ve run into the same exception that I believe is a bug also. But this has to do with the auto-generated proxy class when adding a web reference to a project.

Lets say the proxy class has some sort of Send( basetype ) method on it. The BaseType is defined in the proxy as well. For reasons that are besides the point of this thread, I needed to create a type derived from the base type, which could implement an interface. The derived type is completely blank. The base type completely satisfies the implementation of the interface.

At first I just tried calling proxy.Send( derivedType ) and received the same exception you received. I thought ok, thats a little weird, but figured I would just cast up to the base type ( remember I’m not losing anything from the empty derived type ).

So, I called proxy.Send( ( BaseType ) derivedType )… I am sending the expected type. I still receive this ridiculous exception.

Why would the following work ( work with me on the “all properties” )

BaseType baseType = new BaseType();
baseType.[ all properties ] = derivedType.[ all properties ];
proxy.Send( baseType );

but this not work

BaseType baseType = ( BaseType ) derivedType;
proxy.Send( baseType );

In both cases, I am sending an object whose runtime type is the type expected by the proxy. AHHHHHHHH… This is not a case where XmlInclude or SoapInclude are helpful… I can not add these attributes to the proxy class every time VS autogenerates it ( still in development, proxies are going to be updated fairly consistently ), if this were the case, I might as well just modify the proxy to implement the interface and have no derived type. This is all besides the point… how are the two code samples I gave different?

Seth Flowers
June 27th, 2006

I figured a way around my problem at least. I included partial classes for the BaseType which were empty, but declared that the BaseType implemented my interface ( which they completely satisfied ). This allowed my proxies to be updated during development, since I knew the BaseType wouldn’t change, and I didn’t have to keep modifying the autogenerated proxy to implement and interface.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010