Method Overloading in WCF zt
Method overloading is the process of implementing Polymorphism in Object-Oriented Programming. A method can be overloaded on the basis of type of parameters, number of parameters, and an order of parameters.
As we know, WCF code is always coded on OOP's based programming language so that it does support method overloading.
Service Interface
Collapse | Copy Code[ServiceContract]
public interface ITest
{
[OperationContract]
string TestMethod(int para1,int para2);
//Initail method
[OperationContract]
string TestMethod(string para1, string para2);
//Overloading on the basis of type of parameters.
[OperationContract]
string TestMethod(int para1, string para2);
//Overloading on the basis of an order of parameters.
[OperationContract]
string TestMethod(int para1, string para2,double para3);
//Overloading on the basis of the numbers of parameters
}
Service implementation
Collapse | Copy Codepublic class Test : ITest
{ public string TestMethod(int para1, int para2)
{
return "TestMethod1";
} public string TestMethod(string para1, string para2)
{
return "TestMethod2";
} public string TestMethod(int para1, string para2)
{
return "TestMethod3";
} public string TestMethod(int para1, string para2, double para3)
{
return "TestMethod4";
}
}
Issues of method overloading in WCF
Once test the above code on WCF test client, it will throw an error contract mismatch because of the WSDL that does n't allow to create duplicate methods for clients.
Collapse | Copy CodeError: Cannot obtain Metadata from http://localhost:61489/Test.svc If this is a Windows (R) Communication Foundation
service to which you have access, please check that you have enabled metadata publishing at the specified address.
For help enabling metadata publishing,
please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:61489/Test.svc Metadata contains a reference that cannot
be resolved: 'http://localhost:61489/Test.svc'. The server did not provide a meaningful reply; this might
be caused by a contract mismatch, a premature session shutdown or an internal server error. HTTP GET Error URI: http://localhost:61489/Test.svc There was an error downloading
'http://localhost:61489/Test.svc'. The request failed with the error message:--<html>
<head> <title>The type 'MethOverWCF.Test', provided as the Service attribute
value in the ServiceHost directive, or provided in the configuration element
system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found
Solution of method overloading issue in WCF
By adding unique operationcontract behavior, we can be achieved method overloading in WCF. OperationContract has the Name property that exposes the WCF methods to WSDL Schemas.
Collapse | Copy Code[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
}
Creating Client and Consuming overloaded WCF service methods
In WCF client, all service methods are called by exact same name as define in the OperationContract but method overloaded methods are called by their attribute name. In the given sample code has four different attributes (Method1, Method2, Method3, and Method4) which are exposed by overloaded TestMethod of WCF.
Collapse | Copy Codeusing WCFConsoleClentApp.MyTest;
namespace WCFConsoleClentApp
{
class Program
{
static void Main(string[] args)
{
TestClient client = new TestClient();
string callMethod1 = client.Method1(1, 1);
string callMethod2 = client.Method2("Test", "Test");
string callMethod3 = client.Method3(1, "Test");
string callMethod4 = client.Method4(1, "Test", 3.5);
}
}
}
Method Overloading in WCF zt的更多相关文章
- 方法重载(method overloading)
为什么需要方法重载? 在编程语言中,名字的使用很重要.创建对象的时候,我们给一块内存区域起一个名字,然后这个名字就是我们创建的对象的引用,只要我们"叫"这个名字,计算机就知道我们在 ...
- WCF分布式开发步步为赢(5)服务契约与操作重载
继上一节WCF分布式开发步步为赢系列的(4):WCF服务可靠性传输配置与编程开发,本节我们继续学习WCF分布式开发步步为赢的第(5)节:服务契约与操作重载.这里我们首先讲解OOP面向对象的编程中方法重 ...
- javascript 函数重载 overloading
函数重载 https://en.wikipedia.org/wiki/Function_overloading In some programming languages, function over ...
- How To Easily Call WCF Services Properly z
Please note: this article has been superceded by the documentation for the ChannelAdam WCF Library. ...
- WCF分布式开发步步为赢(7):WCF数据契约与序列化
本节继续学习WCF分布式开发步步为赢(7):WCF数据契约与序列化.数据契约是WCF应用程序开发中一个重要的概念,毫无疑问实现客户端与服务端数据契约的传递中序列化是非常重要的步骤.那么序列化是什么?为 ...
- Part 67 to 70 Talking about method parameters in C#
Part 67 Optional parameters in c# Part 68 Making method parameters optional using method overloadin ...
- Three ways to do WCF instance management
Three ways to do WCF instance management (Per call, Per session, and Single). IntroductionVery often ...
- 【转】《我的WCF之旅》博文系列汇总
转自:http://www.cnblogs.com/artech/archive/2007/09/15/893838.html WCF是构建和运行互联系统的一系列技术的总称,它是建立在Web Serv ...
- Polymorphism & Overloading & Overriding
In Java, a method signature is the method name and the number and type of its parameters. Return typ ...
随机推荐
- C# Unix时间戳转换为时间
在做一些接口的时候,比如返回数据中有一个时间的属性,它的值是使用Unix时间戳表示的,当我们处理它(保存到本地或者格式化前台展示)时需要转换成日期时间,在此就需要根据时间戳转换为日期时间 (注:Uni ...
- javascript闭包的理解
闭包是Javascript的一个难点,但也是一个很重要的知识点. 1.首先我们要知道变量作用域链 变量的作用域分两种:全局变量和局部变量.没有定义到任何函数中的变量为全局变量,在函数中定义的变量为局部 ...
- HDU 4706 Children's Day(简单模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4706 题目大意:要用‘a’-‘z’ 26个小写字母输出倒着写得字母'N'的形状,例如 a ebdfc ...
- Java Lambda简明教程(一)
Lambda表达式背景 许多热门的编程语言如今都有一个叫做lambda或者闭包的语言特性,包括比较经典的函数式编程语言Lisp,Scheme,也有稍微年轻的语言比如JavaScript,Python, ...
- 移动端开发(使用webuploader上传图片,客户端交互,修改alert弹窗等)
之前实习做的一个移动端的页面 需要的功能有图片上传 点击客户端的返回按钮 有提示(即与客户端有交互) 遇到不少的坑 总结一下问题 1.图片上传功能 使用工具 百度的webuploader 暂时遇到的 ...
- Oracle课堂实验一“表的使用”代码。
--创建本地管理表空间CustomerTBSCREATE TABLESPACE CustomerTBS DATAFILE 'd:\Oracle11\product\11.2.0\ora ...
- 如何利用SecureCRT连接Ubuntu12.0.4
环境描述:虚拟机网络选择NAT连接方式,Ubuntu的版本是Ubuntu12.0.4 1. 先做一个测试,假设现在系统还没有装ssh,用secureCRT连接Ubuntu是出现下面的界面. 实际上,这 ...
- AppDelegate解析
当我们创建一个iOS项目,默认会有main.m类,这是一个程序的主入口.main.m方法体如下: #import <UIKit/UIKit.h> #import "AppDele ...
- UIImage拉伸显示
下面张图片,是设计来做按钮背景的: button.png,尺寸为:24x60 现在我们把它用作为按钮背景,按钮尺寸是150x50,以下是没有经过技术性拉伸处理的情况: // 得到view的尺寸 ...
- Google java编程技术规范
不遵循规范的程序猿,不是好的coder. 学习java有一段时间了,一直想找java编程技术规范来学习一下,幸而网络资源丰富,各路玩家乐于分享,省去了好多麻烦,姑且算站在网友的肩上,砥砺前行. /** ...