简体   繁体   English

Web服务版本控制 - 在WCF中向服务合同添加操作

[英]Web Service Versioning - Adding Operations to a Service Contract in WCF

In a WCF service, what happens if I add methods as operation contracts after clients (that consume the service) have completed their implementations? 在WCF服务中,如果在客户端(使用服务)之后将方法添加为操作契约,会发生什么? Will the existing clients have to modify their implementations even though they do not use the new operation contract methods? 现有客户是否必须修改他们的实现,即使他们不使用新的操作合同方法?

EDIT: Will the clients have to update their proxy even though they don't use the new contracts? 编辑:客户是否必须更新他们的代理,即使他们不使用新合同?

No, new operation contracts in the service will not break the client's interface. 不,服务中的新操作合同不会破坏客户端的界面。 You can freely change your interface as long as the method signatures that the client relies on remain unscathed. 只要客户端依赖的方法签名保持未受损害,您就可以自由地更改您的界面。 This means that you can add as many new interface members as you wish. 这意味着您可以根据需要添加任意数量的新界面成员。

The answer to this depends on your point of view. 答案取决于您的观点。 I say that changing the contract at all violates the contract. 我说改变合同违反了合同。 That's why they call them "contracts". 这就是他们称之为“合同”的原因。

Changing the service contract by adding additional operations "breaks" the client because it will change their proxy code. 通过添加其他操作来更改服务合同会“中断”客户端,因为它将更改其代理代码。 In many Enterprise environments, such a change requires a QA pass, even if the existing client code is not calling the new operations. 在许多企业环境中,即使现有客户端代码未调用新操作,此类更改也需要QA通过。 Basically, by adding operations, you are editing the client code. 基本上,通过添加操作,您正在编辑客户端代码。 In that sense, it's clear that QA is required. 从这个意义上讲,很明显QA是必需的。

There's no need to modify the service contract, when you can instead create a new service contract, and have a different service endpoint implement it. 当您可以改为创建新的服务合同,并且具有不同的服务端点来实现它时,则无需修改服务合同。 You can even have the new service implement both the old and new contracts, and share the exact same code to implement the old one with. 您甚至可以让新服务实现新旧合同,并共享完全相同的代码以实现旧的合同。

I'm also one of the old fashioned types who believe you should use a different namespace for a different contract. 我也是老式的类型之一,他们认为你应该为不同的合同使用不同的命名空间。 In at least a picky sense, the old and new contracts are different, so potentially the same name might mean different things between the two. 至少在挑剔的意义上,新旧合同是不同的,因此可能相同的名称可能意味着两者之间的不同。 This can be mitigated by having the new contract derive from the old, so that the old names will remain in the old namespace, yet new names would be in the new namespace. 这可以通过使旧合同从旧合同派生来减轻,以便旧名称将保留在旧名称空间中,但新名称将位于新名称空间中。

I've just implemented a solution to a similar situation. 我刚刚实施了类似情况的解决方案。 Initially I just created a new interface extending the current ServiceContract, using Service Contract Inheritence , updating the endpoint definition to deliver the new derived interface (as suggested in this article ). 最初,我刚刚创建了一个扩展当前ServiceContract的新接口,使用Service Contract Inheritence ,更新端点定义以提供新的派生接口(如本文所述 )。

This was fine for other .net applications that were connecting, those looking for the 'old' interface got that and those looking for the 'new' got that one instead. 对于正在连接的其他.net应用程序来说这很好,那些寻找“旧”界面的人得到了这个,而那些寻找“新”的人则得到了那个。

The problem was that I had a non .net app which was looking for an explicitly hard-coded binding, BasicHttpBinding_IOriginalInterface , but the new service was offering BasicHttpBinding_IDerivedInterface . 问题是我有一个非.net应用程序正在寻找一个明确的硬编码绑定, BasicHttpBinding_IOriginalInterface ,但新服务提供BasicHttpBinding_IDerivedInterface

By unifying both interfaces with a common ServiceContractName [ServiceContract(Name="IOriginalInterface")] , this got around that issue, as recommended by this article . 通过使用公共ServiceContractName [ServiceContract(Name="IOriginalInterface")]统一这两个接口,这就解决了这个问题,正如本文所建议的那样。

If you're concerned about versioning, my advice would be to follow contract-first approach: WSDL should be the one to be versioned, since it's WSDL that you're exposing to your clients when they want to use your service. 如果你担心版本控制,我的建议是遵循契约优先的方法:WSDL应该是版本化的,因为它是你想要使用你的服务时暴露给你的客户的WSDL。 Leaving WSDL to be changed by WCF (or any other web service technology for that matter) without your direct control will sooner or later cause you (or your clients) pain. 在没有您直接控制的情况下让WCF(或任何其他Web服务技术)更改WSDL迟早会导致您(或您的客户)痛苦。

See WCF - contract-first vs. code-first and some suggestions on the workflow. 请参阅WCF - 契约优先与代码优先以及有关工作流的一些建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM