I have two classes as below:
public class BaseClass
{
public BaseClass() { }
public BaseClass(string name)
{
this._name = name;
}
private string _name = "BaseClass";
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
public class InheritedClass : BaseClass
{
public InheritedClass(string name)
: base(name)
{
}
}
then i write a webmethod to use the classes.
public class MyServices : WebService
{
[WebMethod]
public BaseClass HelloWorld()
{
return new InheritedClass("FirstClass");
}
}
now I get an error:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type InheritedClass was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_BaseClass(String n, String ns, BaseClass o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_BaseClass(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.BaseClassSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
I know I can solve the problem by adding this line before the MyServices
[XmlInclude(typeof(InheritedClass))]
But I can't confirm how many "inherited classes" there are in my code, so it seems not fit for me.
Could you show me another way Thank you.

How to use inherited classes in web method?
Stevehat
The type UserProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
The UserProxy class is the class I'm using on the client side which is inheriting from the base user class exposed in the API. I've written the following code to see if I could figure out what is going on:
baseUser = new User();
baseUser = (API.User)this;
Type test = baseUser.GetType();
What is odd is that when I first instantiate the baseUser its type is API.User however after I downcast the userproxy and assign it to that variable the type of baseUser changes to UserProxy which, I think, is what is causing the error I'm seeing. Has anyone run into this or can anyone suggest a fix
Thanks,
- Matt
CzechMate
Al Hancq
Web Service will always have the type info in the metadata associated with the live assembly in production there, either ngen cached or jit compiled.
Now the client is referencing this through the proxy reference it has, which in turn uses the WSDl supplied info. So the point here is that if u can change teh web service, u can support this at server side of the service, with automatic discovery of the inherited classes base parent chain and then traversing a stipulated depth.
Do i make sense
dmkelly10
I can't exactly get why you can't have inherited classes information..
Cheers
atif ahmed
I may be wrong about the reason (haven't looked into it too deeply), but the fact that you should use XmlInclude is correct.
Tom_HDi
But i still get the error as before. If I use the code.
[WebMethod(EnableSession=true)]
[XmlInclude(typeof(BaseClass))]
public BaseClass HelloWorld()
{
return new InheritedClass("FirstClass");
}
}
And if use
[WebMethod(EnableSession=true)]
[XmlInclude(typeof(InheritedClass))]
public BaseClass HelloWorld()
{
return new InheritedClass("FirstClass");
}
}
it works fine.but it doesn't fit for me as I said before.
Sandyee
[SoapRpcService]
[WebServiceBinding("binding name", "namespace", "wsdl location")]
public class MyServices : WebService
{
[WebMethod]
[SoapRpcMethod("binding name")]
public BaseClass HelloWorld()
{
return new InheritedClass("FirstClass");
}
}
OneStrayCat
Fabricio11
Spardeous
try [WebMethod(EnableSession=true)]
[XmlInclude(typeof(BaseClass))]
public BaseClass HelloWorld()
{
return new InheritedClass("FirstClass");
}
}
The XmlInclude, will return the xml for the class type...
regards,
abhi
rwin
Hi,
I had the same problem. (I got "...Use the XmlInclude or SoapInclude attribute to specify types that are not known statically" exception). After I implemented IXmlSerializable in the base class, everything worked fine.
There are some drawbacks, but it could help you..
Pepa