views:

133

answers:

2

I suspect I am being very silly here but I have the following setup

Class MustInherit myBaseClass
'some stuff

End Class

Class myInheritedClassA 
        inherits myBaseClass

'some more stuff
End Class


Class myInheritedClassB 
        inherits myBaseClass

'some more stuff
End Class

I then have a web service that has a method

Function getSomeClass(id) as myBaseClass
'a factory here dependent on the id will generate a myInherited Class
return CType(aInstanceOfInheritedClass, mybaseClass)

End function

Running this results in the following error

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

So my question is, is there any way of 'Widening' the inherited class to the base class so this would work?

EDIT: RE: the suggestion regarding XmlInclude(typeof inheritedClass), currently this method could potentially return a number of types of inherited class (i.e myInheritedClassA and myInheritedClassB) is it case of simply having to add each of the inheritedTypes in this tag?

I have amended the example to hopefully make things clearer

+1  A: 

Have you tried the following, i.e. adding the attribute class SoapInclude for whatever different inherited class GetSomeClass() may return ? (see this MSDN reference)

EDIT: SoapInclude is if you use SOAP encoding, but do use XmlInclude, with the same syntax if your web service is not SOAP-per-se (which it probably is the better for ;-) )

<WebMethod(), SoapRpcMethod, SoapInclude(GetType(myInheritedClass)), _
   SoapInclude(GetType(MustInherit))> 

Function getSomeClass(id) as myBaseClass
'a factory here dependent on the id will generate a myInherited Class
return CType(aInstanceOfInheritedClass, mybaseClass)

End function
mjv
+1  A: 

You have to use XmlIncludeAttribute to specify the actual types to return.

as per msdn XmlIncludeAttribute

When applying the XmlIncludeAttribute, specify the Type of the derived class. When the XmlSerializer serializes objects that include both the base and the derived class, it can then recognize both object types.

You can use the XmlIncludeAttribute to include derived classes in service description documents that are written in the Web Services Description Language (WSDL). For example, if a method returns an Object, apply the XmlIncludeAttribute to the method and specify the actual types to return.

below the sample to apply the atttibute to the web method

<WebMethod(), XmlInclude(GetType(myInheritedClass))>
Function getSomeClass(id) as myBaseClass
     'a factory here dependent on the id will generate a myInherited Class
      return CType(aInstanceOfInheritedClass, mybaseClass)
End function

Cheers

Ramesh Vel

Ramesh Vel
@Ramesh The attribute syntax used is that for C# (OP needs VB.NET), but close enough. Now, good catch, I assumed that the OP was using SOAP encoding, but in case in uses "plain" encoding XmlInclude is the ticket. I'll fix my response accordingly.
mjv
@mjv you are right!. C# just came naturally when answering, since its been several years i stopped vb.... will updated that now...
Ramesh Vel