向 WCF 操作添加新参数:选择?

发布于 2024-08-11 03:39:14 字数 545 浏览 3 评论 0原文

在不要求客户端更新其 WSDL 的情况下向现有操作添加新(可选)参数的最佳方法是什么?我不想更新命名空间来描述新版本的服务契约,因为这应该向后兼容旧客户端。

我应该添加带有新参数的新操作作为重载吗?或者我应该将参数添加到现有操作中?

这是我的操作:

[OperationContract]
MyResponse GetData();

应该是:

[OperationContract]
MyResponse GetData(); 

[OperationContract]
MyResponse GetData(string filter);

或者更简单的,改成这样:

[OperationContract]
MyResponse GetData(string filter);

后一个选项似乎是最好的,根据我的参考书,“对客户端没有影响。新参数初始化为默认值WCF 是否将其初始化为所谓的默认值?如果是这样,默认值是多少?

What's the best way to handle adding a new (optional) parameter to an existing operation without requiring the client to update their WSDL? I do not want to update the namespace to describe a new version of the service contracts, since this should be backwards compatible with older clients.

Should I add a new operation with a new parameter, as an overload? Or should I just add the parameter to the existing operation?

Here is my operation:

[OperationContract]
MyResponse GetData();

Should it be:

[OperationContract]
MyResponse GetData(); 

[OperationContract]
MyResponse GetData(string filter);

Or more simply, just change it to this:

[OperationContract]
MyResponse GetData(string filter);

The latter option seems best, and according to my reference book, "The impact on client is none. New parameters are initialized to default values at the service." Is WCF initializing it to the the so-called default value? If so, what is the default value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-08-18 03:39:14

需要考虑的一件事是不能有两个同名的操作合约。它的序列化方式会引发错误。

最好的方法是使用选项 3(仅添加新参数),并在方法逻辑中考虑到对于尚未更新的客户端来说它是空值。如果这是客户端需要更新的重大更改,请确保整个应用程序不会因为异常而终止。

One thing to take into consideration is that you can't have two OperationContracts with the same name. The way it's serialized it will throw an error.

The best approach is to go with Option 3 (just adding the new parameter) and within the method logic account for it being a null value for those clients that haven't updated yet. If it's a breaking change that the clients will need to update for, make sure to not have the entire application die because of the exception.

吾家有女初长成 2024-08-18 03:39:14

好吧,在使用现有合同后对其进行更改确实违反了所有面向服务的规则;您永远不应该违反现有合同。

实际上,这种情况经常发生,WCF 可以很好地为您处理这种情况。只要您只引入非破坏性更改,现有客户端就可以继续工作。

这可以是:

  • 现有服务合同上的新操作合同
  • DataContract 上的新非必填字段

您尝试做的事情不会起作用,尽管

  1. 您在 WCF 中不能有两个同名的方法- WCF 不是 .NET,并且您不能拥有两个具有相同名称但仅签名不同的方法。不起作用。您需要使用两个独立的、不同的名称。请记住:您的 WCF 方法调用将被转换为 WSDL(Web 服务描述语言)文档来描述服务 - 并且 WSDL 根本不支持具有相同名称的两个操作 - 只是不支持签名不同并且无法工作.

  2. 你不能改变现有的契约,例如,你不能在事后将新的参数引入到方法调用中,而不破坏契约。

    你不能改变现有的契约,

因此,您真正需要做的是:

[OperationContract]
MyResponse GetData(); 

[OperationContract]
MyResponse GetFilteredData(string filter);

您建议的任何其他更改都会 a) 违反合同,或者 b) 根本无法在 WCF 中工作:

Well, changing an existing contract after it's been used is really against all rules of service orientation; you should never ever break an existing contract.

In reality, this happens quite frequently, and WCF is pretty good about handling that for you. As long as you only introduce non-breaking changes, existing clients will continue to work.

This can be:

  • a new operation contract on an existing service contract
  • a new non-required field on a DataContract

What you're trying to do is not going to work, though

  1. you cannot have two method with the same name in WCF - WCF is not .NET and you cannot have two methods by the same name being different only by their signature. Doesn't work. You'll need to use two separate, distinct names. Remember: your WCF method calls will be translated into a WSDL (web service description language) document to describe the service - and WSDL simply does not support having two operations with the same name - just a difference in signature is not supported and will not work.

  2. you cannot change the existing contract, e.g. you cannot introduce a new parameter into a method call after the fact, without breaking the contract.

So what you really need to do is this:

[OperationContract]
MyResponse GetData(); 

[OperationContract]
MyResponse GetFilteredData(string filter);

Any other change you suggested will a) break the contract, or b) simply not work in WCF:

新一帅帅 2024-08-18 03:39:14

你可以试试这个:

[OperationContract]
MyResponse GetData(); 

[OperationContract(Name = "GetDataByFilter")]
MyResponse GetData(string filter);

you can try this:

[OperationContract]
MyResponse GetData(); 

[OperationContract(Name = "GetDataByFilter")]
MyResponse GetData(string filter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文