Web services and derived classes
When doing this in an ASP.NET 2.0 (and I imagine ASP.NET 1.1 functions similarly)
1
2
3
4
5
6
7
8
9
10
11
12
13
[WebMethod]
public MyBase SayHello()
{
return new MyDerivedClass();
}
public class MyBase
{
}
public class MyDerivedClass : MyBase
{
}
I get hit with this exception:
1
2
3
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.
This post is licensed under CC BY 4.0 by the author.