[英]Why are my base class fields not being serialized when returning derived class object from an ASMX web method?
I have two classes and a web method as follows 我有两个类和一个web方法如下
[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 fields from the base class. 但是当我看到SOAP响应时,我没有看到基类中的字段。 Are they no supposed to be serialized? 它们不应该被序列化吗? If I wanted them to be serialized what should be done? 如果我希望它们被序列化应该做什么?
You can remove the [Serializable]
attributes from your classes, this should work without. 您可以从类中删除[Serializable]
属性,这应该没有。 POCOs don't require the attribute to be present, they serialize fine as they are. POCO不需要存在属性,它们按原样序列化。
Edit : Have you actually checked what the web service output is, or are you just looking at the message definition on the service web endpoint? 编辑 :您是否实际检查了Web服务输出是什么,或者您只是查看服务Web端点上的消息定义?
I can see that the SOAP message format generated when you browse to the service endpoint in your browser doesn't seem to know anything about the base class fields: 我可以看到,当您浏览浏览器中的服务端点时生成的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>
But when you call the web service using the test form, or Storm , the class gets serialized: 但是当您使用测试表单或Storm调用Web服务时,该类将被序列化:
<DerivedClass>
<Key>1</Key>
<IsModified>true</IsModified>
<IsNew>true</IsNew>
<IsDeleted>true</IsDeleted>
<Name>Test1</Name>
</DerivedClass>
The visual studio "Add web reference" dialog also creates the proxy classes correctly. visual studio“添加Web引用”对话框还可以正确创建代理类。
Edit 2 : Looking at the web service definition (service.asmx?wsdl) generated for us, we can see that the definition preserves the original inheritance hierarchy, as opposed to flattening the object down to its fields in the serialization process: 编辑2 :查看为我们生成的Web服务定义(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>
Interestingly enough it seems that the generated sample message at the service endpoint doesn't take the extension into account. 有趣的是,似乎服务端点上生成的示例消息不考虑扩展。 But for all other effects and purposes your code should work. 但是对于所有其他效果和目的,您的代码应该可行。
You have to add KnownType attribute to base class . 您必须将KnownType属性添加到基类。
[Serializable]
[KnownType(typeof(DerivedClass )]
public class BaseClass
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.