【发布时间】:2010-04-02 20:52:12
【问题描述】:
我正在升级我们的网络服务以支持版本控制。我们将发布我们的版本化网络服务,如下所示:
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 指的是XmlAttribute 或 System.Xml 命名空间的其他部分,它允许我控制正在序列化的对象属性的类型(取决于我正在序列化的对象的版本)。
有谁知道如何做到这一点?
【问题讨论】:
标签: c# .net web-services xml-serialization asmx