Web 服务 - XmlInclude 在派生类而不是基类中? [英] Web services - XmlInclude in a derived class instead of a base class?

查看:25
本文介绍了Web 服务 - XmlInclude 在派生类而不是基类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Web 服务调用中使用抽象类作为参数.目前,我在基类中包含派生类的 XmlInclude,如下所示:

I am using an abstract class as a parameter in a web service call. Currently, I am including an XmlInclude of a derived class in the base class, like so:

[XmlInclude(typeof(DerivedClass))]
public abstract class BaseClass
{
}

但是,我不想在基类中包含所有派生类型.

However, I'd rather not include all of the derived types in the base class.

http://www.multiplesight.com/community/blogs/craig/archive/2004/07/08/1580.aspx,作者提到了一个替代方案——将属性写在web方法之上,如下所示:

In http://www.pluralsight.com/community/blogs/craig/archive/2004/07/08/1580.aspx, the author mentions an alternative - writing the attribute above the web method instead, like so:

[WebMethod]
[System.Xml.Serialization.XmlInclude(typeof(DerivedClass))]
public BaseClass getClass() {
     return new DerivedClass(); 
}

但是,我也不想将派生类型放在 Web 服务中.有没有办法将属性保留在派生类型中?

However, I'd also like to not put the derived types in the web service either. Is there a way of keeping the attribute in the derived type?

推荐答案

让我们假设框架在反序列化发生时需要以某种方式知道类型层次结构中的类型,以及这些类型如何在 xml 中表示.如果它存储在派生类型中,它确实无法推断此信息.

Lets take it as a given that the framework would somehow need to know what types are in the type hiearchy when deserialization occurs, and how those types are represented in xml. It really has no way to infer this information if it is stored in a derived type.

然后您有几个选择:- 使用 XmlInclude 属性- 在 XmlSerializer 构造函数重载中指定允许的类型集

You then have a couple of options: - use the XmlInclude attribute - specify the set of allowed types in the XmlSerializer Constructor overload

现在,如果您希望将子类传递给网络服务,网络服务器将控制序列化和反序列化.所以 XmlSerializer 构造函数不再是一个选项.

Now, if you're expecting a subclass to be passed in to the webservice, the webserver controls serialization and deserialization. So the XmlSerializer contstructor is no longer an option.

如你所说,你可以把属性放在webservice方法上,而不是直接放在类上.在保持类纯"和记住将这些属性放在每个可能需要的地方之间需要权衡.

As you say, you can put the attribute on the webservice method instead of directly on the class. There's a trade-off between keeping your class "pure" and remembering to put those attributes in every place they may be required.

当然,真正的问题似乎是您试图将业务对象用作 Web 服务层中的消息格式.

Of course, the real problem appears to be that you are trying to use your business objects as the message format in your webservice layer.

如果您真的希望将消息格式"和业务对象"职责分开,那么可以使用另一个类(具有完整的层次结构),该类的唯一用途是用作 Web 服务参数.在这种情况下,在基类上粘贴所有您需要的 XmlInclude 属性没有问题.然后,在调用 Web 服务时,根据消息格式对象调整您的业务对象.这为您提供了一个额外的好处,即不对您的参数类型应用网络服务类型约束(例如,没有接口作为参数).

If you really want to keep the "message format" and "business object" responsibilities separate, then have another class (with the full hierarchy) for which the only use is to be used as a webservice parameter. In that case, there's no problem with sticking all the XmlInclude attributes you need, on the base class. Then, when making calls to the webservice, adapt your business object to and from the message format object. This gives you the added benefit of not applying webservice type constraints to the types of your parameters (eg no interfaces as parameters).

当然,这种方法没有那么方便.

Of course, this method isn't as convenient.

最后,webservice 需要知道预期的类型,否则将无法正确序列化和反序列化它们.

In the end, the webservice needs to know what types to expect, or it won't be able to serialize and deserialize them properly.

是的,这是对为什么答案是否定的冗长解释,您不能只将属性保留在派生类型中.虽然我很想错:)

And yes, this is a long winded explanation of why the answer is no, you can't only keep the attribute in the derived type. I'd love to be wrong though :)

这篇关于Web 服务 - XmlInclude 在派生类而不是基类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆