我正在升级我们的Web服务以支持版本控制。我们将发布我们的版本的Web服务,像这样:这个版本的.NET asmx Web服务:将对象属性序列化为字符串属性以支持版本化
http://localhost/project/services/1.0/service.asmx
http://localhost/project/services/1.1/service.asmx
的一个要求是,我不能打破原有的WSDL(1.0 WSDL)。面临的挑战在于如何通过Web服务背后的逻辑来处理新版本的类(该逻辑包括许多命令和适配器类)。请注意,升级到WCF目前不是一个选项。
为了说明这一点,我们来看一个博客和帖子的例子。在引入版本之前,我们传递了具体的对象而不是接口。所以AddPostToBlog
命令将采取Post
对象而不是IPost
。
// Old AddPostToBlog constructor.
public AddPostToBlog(Blog blog, Post post) {
// constructor body
}
随着引进的版本,我想保持原有的Post
,同时增加了PostOnePointOne
。 Post
和PostOnePointOne
都将实现IPost
接口(它们没有扩展抽象类,因为继承打破了wsdl,尽管我想通过一些奇妙的xml序列化技巧可以解决这个问题)。
// New AddPostToBlog constructor.
public AddPostToBlog(Blog blog, IPost post) {
// constructor body
}
这给我带来了关于序列化的问题。原始Post
类具有名为Type
的枚举属性。对于各种跨平台兼容性问题,我们将我们的Web服务中的枚举更改为字符串。所以,我想做到以下几点:
// New IPost interface.
public interface IPost
{
object Type { get; set; }
}
// Original Post object.
public Post
{
// The purpose of this attribute would be to maintain how
// the enum currently is serialized even though now the
// type is an object instead of an enum (internally the
// object actually is an enum here, but it is exposed as
// an object to implement the interface).
[XmlMagic(SerializeAsEnum)]
object Type { get; set; }
}
// New version of Post object
public PostOnePointOne
{
// The purpose of this attribute would be to force
// serialization as a string even though it is an object.
[XmlMagic(SerializeAsString)]
object Type { get; set; }
}
的XmlMagic
指System.Xml命名空间的XmlAttribute
或其他部分,让我来控制的对象属性的类型被序列化(取决于我正在序列化的对象的版本)。
有谁知道如何做到这一点?