提供向后兼容的WCF服务 [英] Making Backward-Compatible WCF Services

查看:71
本文介绍了提供向后兼容的WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR:如何创建向后兼容的WCF服务-也就是说,当我在服务器端部署服务的新版本时,旧版本上的所有客户端都可以仍然使用该服务.

TLDR: How do I create WCF services that are backward compatible -- that is, when I deploy a new version of the service on the server-side, all the clients on the older versions can still use the service.

我正在创建一个Web服务,该服务将允许客户端应用程序获取插件列表.我至少要有一个像FindPlugins(string nameOrDescription)这样的操作,它将在服务器上进行搜索并返回对象列表.

I'm creating a web service that will allow the client applications to fetch a listing of plugins. I will at least have one operation like FindPlugins(string nameOrDescription) which will, on the server, do a search and return a list of objects.

不幸的是,我不能保证我的客户都会随着我的服务的每个新发行版而更新;不,我确定,其中许多将跟踪最新版本,并且将具有旧版本-多大年龄,我不能确定,但​​我知道它们将是旧的:)

Unfortunately, I cannot guarantee that my clients will all be updated with each new release of my service; nay, I am sure that many of them will be trailing the latest version, and will have old versions -- how old, I cannot be sure, but I know they will be old :)

如果我创建一个新的服务操作,更改架构或在服务器端进行某种中断操作,我就完成了.我需要随时设计向后兼容性.

If I create a new service operation, change the schema, or make some sort of breaking operation on the server side, I'm done. I need to engineer backward compatibility at all times.

这是一个例子.假设我返回了一个Plugin的列表,每个列表都有名称和描述,并且部署了我的服务v0.1.然后,我添加一个下载链接,并将其部署为我的服务v0.2.

Here's one example. Say I return a list of Plugins, each which has a name and description, and I deploy v0.1 of my service. Then, I add a download link, and deploy that as v0.2 of my service.

我看到的一些选项是:

  • 强制客户端更新到最新服务(不可行)
  • 中断旧客户的服务(不可行)
  • 在每个操作后附加一个版本号,并且仅使用特定于版本的操作(例如FindPluginsV1FindPluginsV2)-对于多个操作来说似乎不可行
  • 在每个新版本中提供新服务-似乎不可行
  • Force clients to update to the latest service (not feasible)
  • Break the service for old clients (not feasible)
  • Append a version number to each operation and only consume the version-specific operations (eg. FindPluginsV1, FindPluginsV2) -- doesn't seem practical with multiple operations
  • Provide a new service with each new version -- doesn't seem practical

推荐答案

默认情况下,WCF向后兼容.

以下MSDN链接包含WCF合同的所有可能更改的列表,并描述了它们对旧客户端的影响:

The following MSDN link contains a list of all the possible changes of a WCF contract and describes their effect on old clients:

最重要的是,以下操作将不会导致旧客户端崩溃:

Most importantly, the following operations will not cause old clients to break:

服务合同(方法)

  • 添加方法参数:从旧客户端调用时将使用默认值.
  • 删除方法参数:旧客户端发送的值将被静默忽略.
  • 添加新方法:显然,老客户不会调用它们,因为他们不认识它们.

数据合同(用于传递数据的自定义类)

  • 添加不需要的属性.
  • 删除不需要的属性.

因此,除非您将新的DownloadLink字段标记为

Thus, unless you mark the new DownloadLink field as IsRequired (default is false), your change should be fine.

这篇关于提供向后兼容的WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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