简体   繁体   English

从ASMX Web方法返回派生类对象时,为什么我的基类字段没有被序列化?

[英]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.

相关问题 ASMX Web服务的基类-自行调用从基类派生的方法 - Base class for an ASMX webservice - calling the derived method from the base class it self 从基类返回派生类实例 - Returning derived class instance from a base class 当从基类派生类的对象上调用时,“ this”关键字类型 - “this” keyword type when called on an object of derived class from base class 为什么我的派生类不能与基类融合? - Why is my derived class not converible to base class? 从基础之外的方法返回任何派生的 class 类型和派生的 class - Returning any derived class type from a method outside the base and derived class 从基类方法克隆派生类 - Clone derived class from base class method 为什么派生类字段在基类字段之前初始化 - Why are derived class fields initialized before the base class fields 从派生/基类的对象调用抽象方法 - Calling abstract method from an object of derived/base class 是否可以使用Mongo DB C#驱动程序序列化基类中的属性或字段? - Can I exclude properties or fields in a base class from being serialized with Mongo DB C# driver? 当第一次访问静态类是基类上的静态方法时,为什么我的静态对象没有被实例化? - Why are my static objects not being instantiated when first access to the static class is a static method on the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM