WCF函数重载 [英] WCF Function Overloading

查看:58
本文介绍了WCF函数重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道WCF不支持方法重载,为了实现它,我们需要用name属性修饰operationcontract,即



I know WCF does not support method overloading and to achieve it we need to decorate the operationcontract with name attribute i.e.

[ServiceContract ]
public interface ITest
{
    [OperationContract(Name="Method1")]
    string TestMethod(int para1,int para2);
    //Initail method
    [OperationContract(Name = "Method2")]
    string TestMethod(string para1, string para2);
    //Overloading on the basis of type of parameters.
    [OperationContract(Name = "Method3")]
    string TestMethod(int para1, string para2);
    //Overloading on the basis of  an order of parameters.
    [OperationContract(Name = "Method4")]
    string TestMethod(int para1, string para2,double para3);
    //Overloading on the basis of the numbers of parameters
}



我的问题是它是如何方法oveloding,因为我们需要在每次调用该方法时使用Method2调用第二个TestMethod,以便为什么要使用属性进行装饰,为什么不简单地像Method1一样编写,方法2等

作为开发人员,我们使用Name属性的好处是什么?


my question is how it is method oveloding as we need to call second TestMethod with Method2 whenever we want to call that method so why decorate with attribute and why not simply write like Method1,Method2 etc.
what is the benefit as a developer we are getting to use the Name attribute ??

推荐答案

你可以像你一样比如命名方法Method1,Method2,而不是根据您的示例代码进行装饰。从客户端来看它似乎没有什么不同。

实际上在客户端的好处是,被覆盖的方法具有语义含义 - 这些方法同样允许不同的输入参数。代码使用 ITest 通过实际方法名称直接(而不是通过WCF)调用。使用WCF,您始终必须知道该服务是使用基于OO的代码编写的,但服务调用本身不是OO,并且此处存在不匹配。



我暂时没有在愤怒中使用WCF - 但我曾经尽可能地尝试保留合同名称(在线路上下传递的名称)和服务代码中的实际方法名称相同。 (数据合同相同)否则我发现通过等方式追踪问题更难。
You could, as you say name the methods "Method1", "Method2" instead of decorating as per your example code. From the client side it appears no different.
The benefit is really on the client side, the overridden methods have a semantic meaning - these methods do the same allow for the different input parameters. Code using ITest directly (not via WCF) calls via the actual method names. With WCF you always have to be aware that the service is written in OO-based code, but the service calls itself isn't OO and there is a mismatch here.

I haven't used WCF in anger in a while - but I did used to try as far as possible to keep the contract name (the one that is passded up and down the wire) and the actual method name in the service code the same. (Same for data contracts) otherwise I found it harder to trace problems through etc.


这篇关于WCF函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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