Why are the fields of the base class not serialized when the object of the derived class is returned from the ASMX web method?

I have two classes and a web method as follows

[Serializable]
public class BaseClass
{
    public int  Key;
    public bool IsModified;
    public bool IsNew;
    public bool IsDeleted;
}

[Serializable]
public class DerivedClass : BaseClass
{
    public string Name;
}

[WebMethod]
public List<DerivedClass> GetDerivedClassObjects()
{

}

But when I see the SOAP response, I do not see the field from the base class. Shouldn't they be serialized? If I wanted them to be serialized, what should I do?

+5
source share
2 answers

You can remove attributes [Serializable]from your classes, this should work without. POCOs do not require the attribute to be present; they are serialized as accurately as possible.

. , -, -?

, SOAP, , , :

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetDerivedClassObjectsResponse xmlns="http://tempuri.org/">
      <GetDerivedClassObjectsResult>
        <DerivedClass>
          <Name>string</Name>
        </DerivedClass>
        <DerivedClass>
          <Name>string</Name>
        </DerivedClass>
      </GetDerivedClassObjectsResult>
    </GetDerivedClassObjectsResponse>
  </soap:Body>
</soap:Envelope>

- Storm, :

<DerivedClass>
    <Key>1</Key> 
    <IsModified>true</IsModified> 
    <IsNew>true</IsNew> 
    <IsDeleted>true</IsDeleted> 
    <Name>Test1</Name> 
</DerivedClass>

" -" -.

2. - (service.asmx? wsdl), , , , :

<s:complexType name="DerivedClass">
    <s:complexContent mixed="false">
        <s:extension base="tns:BaseClass">
            <s:sequence>
                <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" /> 
            </s:sequence>
        </s:extension>
    </s:complexContent>
</s:complexType>
<s:complexType name="BaseClass">
    <s:sequence>
        <s:element minOccurs="1" maxOccurs="1" name="Key" type="s:int" /> 
        <s:element minOccurs="1" maxOccurs="1" name="IsModified" type="s:boolean" /> 
        <s:element minOccurs="1" maxOccurs="1" name="IsNew" type="s:boolean" /> 
        <s:element minOccurs="1" maxOccurs="1" name="IsDeleted" type="s:boolean" /> 
    </s:sequence>
</s:complexType>

, . .

+3

KnownType .

[Serializable]
[KnownType(typeof(DerivedClass )]
public class BaseClass
{
   ...
}
+1

All Articles