如何更改不影响客户端的 WCF 服务合同? [英] How to change WCF service contract which does not effect client?

查看:26
本文介绍了如何更改不影响客户端的 WCF 服务合同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,但是读了一些关于 wcf 的文章我才知道,如果你改变了 ServcieContrcat,那么你不仅要改变服务端,还要改变客户端,这真的很难管理.

i am new but reading some article on wcf i came to know that if you change the ServcieContrcat then you have to change not only the Service end but Clients end too and it's really difficult to manage.

Example 1:

开发者必须为订单处理创建 WCF 服务,具有以下功能:GetOrderById、GetOrdersByStatus、SaveOrder

Developer have to create WCF service for Order processing, with following function: GetOrderById, GetOrdersByStatus, SaveOrder

ServiceContract 可能如下所示

The ServiceContract could looks like following

[ServiceContract]
public interface IOrderService
{
    [OperationContract]
    Order GetOrderById(int orderId);

    [OperationContract]
    List<Order> GetOrdersByStatus(OrderStatus orderStatus);

    [OperationContract]
    void SaveOrder(Order order)
} 

例如,一个月后,项目经理说:好的,我们的客户需要另一个函数:DeleteOrderById、GetOrdersByCustomerId 并且不再需要 GetOrdersByStatus,我们需要 GetOrdersByStatusAndCustomerId

after month, for example, project manager say: Ok, our customers need another functions: DeleteOrderById, GetOrdersByCustomerId and don't need GetOrdersByStatus any more, we need GetOrdersByStatusAndCustomerId

开发者必须更新 ServiceContrcat 和更新客户端.如您所见,ServiceContrcat 中的任何更改都非常困难

Developers have to update ServiceContrcat and update client. As you can see, any changes in the ServiceContrcat is really difficult

所以我正在寻找如何开发 wcf 服务的最佳指导,如果我们扩展功能或进行任何类型的更改,它不会产生任何问题,但客户端不会遇到任何问题.谢谢

so i am looking for best guidance how to develop wcf service which will not create any problem if we extend the functionality or any kind of change but client end will not face any problem. thanks

推荐答案

我有同样的问题,基本上是对方法的一组永久更改,这些更改会破坏客户端上的接口.

I had the same problem, basically a perpetual set of changes to methods that would break the interfaces on the clients.

这就是我所做的:我所有的 30 多个函数(以及列表不断增长)都将字符串、整数和字节()作为数据类型作为输入和输出参数,我创建了一个主单一端点和函数接收,作为输入参数,并作为输出发回,一个简单的类.这个名为 HostInterface 的类只包含两个参数,一个字符串(我用它来封装我所有的字符串和整数)和一个 byte() 数组(我把所有的二进制文件都塞进去)

This is what I did: All of my 30+ functions (and the list grows and grows) took strings, ints and bytes() as data types as both input and output parameters, I created a master single endpoint and function that receives, as an input parameter, and sends back as the output, a single simple class. This class, called HostInterface, contains just two parameters, a string (which I use to encapsulate all my strings and ints) and a byte() array (into which I stuff all my binaries)

因此,无论客户端是调用 Ping() 之类的简单函数,只使用一个字符串参数,还是 ResumeDownload() 之类的复杂函数,它具有 5 个字符串、2 个整数和一个字节数组,所有这些参数都封装在我的 HostInterface 类中,将字符串和整数转换为一个字符串参数(如 XML)并将字节转换为字节字段.

So, whether a client is calling something simple like Ping() with just one string param, or something complicated like ResumeDownload() with 5 strings, 2 ints and a byte array, all of those parameters get encapsulated in my HostInterface class, the strings and ints into the one String parameters (as XML) and the bytes into the byte field.

当这里主机端收到请求时:

When a request is received on the host side here:

  Function HostConnect(byval objInbound as HostInterface) as HostInterface

我将 objInbound 中的字符串参数解包,将 XML 更改为一个对象,然后将字节解包并将它们添加到同一对象的字节部分.然后我检查方法名称(ping 或 ResumeDownload)并进行相应处理.下图显示了基本思想 - 所有函数都通过一个函数运行,该函数接受和发送相同的简单类作为参数.因此,我的界面永远不需要改变.

I unpack the string parameter in objInbound, changing the XML into an object, and I unpack the bytes and add them to the byte portion of the same object. Then I check the method name (ping or ResumeDownload) and process accordingly. The diagram below shows the basic idea - all functions operating through a single function that takes and sends the same simple classe as parameters. Thus, my interface never needs to change.

这篇关于如何更改不影响客户端的 WCF 服务合同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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