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 Code
public 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 Code
Error: 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 Code
using 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的更多相关文章

  1. 方法重载(method overloading)

    为什么需要方法重载? 在编程语言中,名字的使用很重要.创建对象的时候,我们给一块内存区域起一个名字,然后这个名字就是我们创建的对象的引用,只要我们"叫"这个名字,计算机就知道我们在 ...

  2. WCF分布式开发步步为赢(5)服务契约与操作重载

    继上一节WCF分布式开发步步为赢系列的(4):WCF服务可靠性传输配置与编程开发,本节我们继续学习WCF分布式开发步步为赢的第(5)节:服务契约与操作重载.这里我们首先讲解OOP面向对象的编程中方法重 ...

  3. javascript 函数重载 overloading

    函数重载 https://en.wikipedia.org/wiki/Function_overloading In some programming languages, function over ...

  4. How To Easily Call WCF Services Properly z

    Please note: this article has been superceded by the documentation for the ChannelAdam WCF Library. ...

  5. WCF分布式开发步步为赢(7):WCF数据契约与序列化

    本节继续学习WCF分布式开发步步为赢(7):WCF数据契约与序列化.数据契约是WCF应用程序开发中一个重要的概念,毫无疑问实现客户端与服务端数据契约的传递中序列化是非常重要的步骤.那么序列化是什么?为 ...

  6. 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 ...

  7. Three ways to do WCF instance management

    Three ways to do WCF instance management (Per call, Per session, and Single). IntroductionVery often ...

  8. 【转】《我的WCF之旅》博文系列汇总

    转自:http://www.cnblogs.com/artech/archive/2007/09/15/893838.html WCF是构建和运行互联系统的一系列技术的总称,它是建立在Web Serv ...

  9. Polymorphism & Overloading & Overriding

    In Java, a method signature is the method name and the number and type of its parameters. Return typ ...

随机推荐

  1. javaScript笔记1

    一.通过 id 访问HTML元素,可以使用 document.getElementById(id) 方法. 例子: <body> <button id="mybtn&quo ...

  2. 2015-01-27-从实验出发理解buffer与cache区别-吴伟顺

        通过du(find) 与 cat 体现buffer与cache差异实验: 实验表明: 1 通常 buffer << cache 2 "文件系统"相关内容(ino ...

  3. Jquery环境搭建前言

  4. 深入了解float

    1.float的历史   初衷是为了图片的文字环绕,将img设置float 2.破坏性与包裹性  a.父元素没有设置高度,内部元素浮动后,服务元素的高度被破坏了,可以将其父元素设置overflow:h ...

  5. Visual Studio2012中搭建WCF项目

    分布式系统:指在系统与系统之间进行通信,系统不再是孤立的,例如:淘宝查看物流信息,或是hao123的天气预报,这些可能都是用的别的系统的web方法. 1.创建空的解决方案 2.新建项目-WCF服务库项 ...

  6. PHP正则表达式 验证电子邮件地址

    我们最经常遇到的验证,就是电子邮件地址验证.网站上常见.各种网页脚本也都常用“正则表达式”(regular expression)对我们输入的电子邮件地址进行验证,判断是否合法.有的还能分解出用户名和 ...

  7. python之PIL安装问题

    ··在windows安装模块 总是出现问题,今天安装PIL的 首先提示我的是pip命令出错,这应该是当你安装Python2.7的时候 并没有把pip模块添加进去 导致出现了这样的一个问题,为了省事,我 ...

  8. 多目标遗传算法 ------ NSGA-II (部分源码解析)状态报告 打印 report.c

    /* Routines for storing population data into files */ # include <stdio.h> # include <stdlib ...

  9. iOS开发之自定义画板

    今天整好有时间, 写了一个自定义的画板!  [我的github] GLPaint主要采用QuartzCore框架, 对画布上的元素进行渲染, 然后通过UIImageWriteToSavedPhotos ...

  10. 导航 -MapKit - 获取路线信息绘制导航路线

    #import "PPViewController.h" #import <MapKit/MapKit.h> #import "PPAnnotation.h& ...