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

查看:233
本文介绍了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.pluralsight.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(); 
}



不过,我也想不把派生类型的网页无论是服务。是否有保持属性在派生类的方法吗?

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?

推荐答案

让我们把它作为该框架将在某种程度上需要给出要知道是什么类型的类型hiearchy反序列化时发生,而这些类型如何在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

现在,如果你期待一个子类中的Web服务,Web服务器传递控制序列化和反序列化。所以XmlSerializer的contstructor不再是一个选项。

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.

就像你说的,你可以把属性上的web服务的方法,而不是直接在类。还有让你的类纯,并记住把这些属性在每一个他们可能需要的地方之间的权衡。

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属性需要,在基类中没有问题。然后,打电话给webservice时,调整你的业务对象和消息格式对象。这使您不适用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.

在最后,Web服务需要知道会发生什么类型的,否则将无法序列化和反序列化正确它们。

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天全站免登陆