一、服务契约(包括回调契约)通过指定不同的OperationContract.Name来实现重载方法,当然代码部份还是必需符合C#的重载要求,即相同方法名称,不同的参数个数或参数类型

namespace Contract
{
[ServiceContract(Name = "HellworldService", Namespace = "http://www.zuowenjun.cn")]
public interface IHelloWorld
{
[OperationContract(Name = "GetHelloWorldWithoutParam")]
string GetHelloWorld(); [OperationContract(Name = "GetHelloWorldWithParam")]
string GetHelloWorld(string name);
}
}

二、创建服务寄宿程序与不含重载方法的WCF服务相同,在此不再重述。

三、在客户端调用WCF服务

A.若直接通过引用WCF服务的方法生成的相关服务类,则使用方法如下(说明:服务方法名称不再是我们之前定义的方法名,而是OperationContract.Name)

namespace Client
{
class Program
{
static void Main(string[] args)
{
using (HellworldServiceClient helloWorldProxy = new HellworldServiceClient())
{
Console.WriteLine("服务返回的结果是: {0}", helloWorldProxy.GetHelloWorldWithoutParam());
Console.WriteLine("服务返回的结果是: {0}", helloWorldProxy.GetHelloWorldWithParam("Zuowenjun"));
} Console.ReadLine();
}
}
}

B.若想保持与服务契约定义的方法名称相同,做到真正意义上的方法重载,则可自己实现客户端代理类,而不是通过引用后由VS代码生成。

using Contract;
using System.ServiceModel;
namespace Client
{
class HellworldServiceClient : ClientBase<IHelloWorld>, IHelloWorld
{
#region IHelloWorld Members
public string GetHelloWorld()
{
return this.Channel.GetHelloWorld();
} public string GetHelloWorld(string name)
{
return this.Channel.GetHelloWorld(name);
}
#endregion
}
}

注意:IHelloWorld服务契约接口必须先自己重新定义或引用之前服务契约接口类库,建议将服务契约接口单独放在一个类库,这样就可以减少重写接口的时间。

同理,若想保持服务契约接口的继承,也需要在客户端重新定义服务契约及客户端服务代理类,这里直接引用Learning hard的文章内容:

// 自定义代理类
public class SimpleInstrumentationClient : ClientBase<ICompleteInstrumentation>, ISimpleInstrumentation
{
#region ISimpleInstrumentation Members
public string WriteEventLog()
{
return this.Channel.WriteEventLog();
}
#endregion
} public class CompleteInstrumentationClient:SimpleInstrumentationClient, ICompleteInstrumentation
{
public string IncreatePerformanceCounter()
{
return this.Channel.IncreatePerformanceCounter();
}
} //调用服务
class Program
{
static void Main(string[] args)
{
using (SimpleInstrumentationClient proxy1 = new SimpleInstrumentationClient())
{
Console.WriteLine(proxy1.WriteEventLog());
}
using (CompleteInstrumentationClient proxy2 = new CompleteInstrumentationClient())
{
Console.WriteLine(proxy2.IncreatePerformanceCounter());
} Console.Read();
}
}

当然也可以通过配置不同的endpoint终结点的,contract设为相应的服务契约接口

<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:9003/instrumentationService/mex"
binding="mexHttpBinding" contract="ClientConsoleApp.ISimpleInstrumentation"
name="ISimpleInstrumentation" />
<endpoint address="http://localhost:9003/instrumentationService/mex"
binding="mexHttpBinding" contract="ClientConsoleApp.ICompleteInstrumentation"
name="ICompleteInstrumentation" />
</client>
</system.serviceModel>
</configuration>

调用如下:

class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ISimpleInstrumentation> simpleChannelFactory = new ChannelFactory<ISimpleInstrumentation>("ISimpleInstrumentation"))
{
ISimpleInstrumentation simpleProxy = simpleChannelFactory.CreateChannel();
using (simpleProxy as IDisposable)
{
Console.WriteLine(simpleProxy.WriteEventLog());
}
}
using (ChannelFactory<ICompleteInstrumentation> completeChannelFactor = new ChannelFactory<ICompleteInstrumentation>("ICompleteInstrumentation"))
{
ICompleteInstrumentation completeProxy = completeChannelFactor.CreateChannel();
using (completeProxy as IDisposable)
{
Console.WriteLine(completeProxy.IncreatePerformanceCounter());
}
} Console.Read();
}
}

这篇文章参考和引用如下作者的文章内容:

跟我一起学WCF(5)——深入解析服务契约[上篇]

跟我一起学WCF(6)——深入解析服务契约[下篇]

文章同步发表于我的个人网站:http://www.zuowenjun.cn/post/2015/03/25/135.html

WCF实现方法重载的更多相关文章

  1. WCF之操作重载

    服务契约的方法重载,会在装载宿主时,抛出异常. 解决是在操作契约上Name设置为不同值,但是生成的代理会把Name的名称作为方法的名称,不过我们可以手动的修改代理类,使得方法名与服务声明的名称一样. ...

  2. java方法重载(overload)、重写(override);this、super关键简介

    一.方法重载: 条件:必须在一个类中,方法名称相同,参数列表不同(包括:数据类型.顺序.个数),典型案例构 造方重载.  注意:与返回值无关 二.方法重写: 条件: (1)继承某个类或实现某接口 (2 ...

  3. Java之方法重载篇(我重载了,你要如何来调用我。。)

      一.课前引言 请看一下代码,你发现什么特殊之处了吗? public class MethodOverload { public static void main(String[] args) { ...

  4. 【java开发】方法重写和方法重载概述

    类的继承   父类-子类 关键字 extends 新建一个父类 public class Person {     private String name;          private int ...

  5. 方法构造和方法重载之奥特曼与大boss之战

    知识点的总结: 1.类中的方法分为两类:1.普通方法: 2.构造方法. 2.构造方法的格式:  public 类名(数据类型  参数名,...){ } 3.构造方法的用途:  1.实例化对象.  2. ...

  6. JavaScript中的方法重载

    对js有些了解的人都知道,在js中根本就不存在像C#中的那种方法重载,而有的只是方法的覆盖,当你在js中敲入两个或多个同名的方法的时候,不管方法(函数)的参数个数怎么个不同,这个方法名只能属于最后定义 ...

  7. PHP面向对象编程——深入理解方法重载与方法覆盖(多态)

    什么是多态? 多态(Polymorphism)按字面的意思就是“多种状态”.在面向对象语言中,接口的多种不同的实现方式即为多态.引用Charlie Calverts对多态的描述——多态性是允许你将父对 ...

  8. c#方法重载,可选参数,命名参数。

    其实这里没什么可说哦,c++的语法大同小异.先看一段代码. class Program { public static void Test(int a) { Console.WriteLine(&qu ...

  9. java中的方法重载与重写以及方法修饰符

    1. 方法重载Overloading , 是在一个类中,有多个方法,这些方法的名字相同,但是具有不同的参数列表,和返回值 重载的时候,方法名要一样,但是参数类型和参数个数不一样,返回值类型可以相同,也 ...

随机推荐

  1. JS写的排序算法演示

    看到网上有老外写的,就拿起自已之前完成的jmgraph画图组件也写了一个.想了解jmgraph的请移步:https://github.com/jiamao/jmgraph 当前演示请查看:http:/ ...

  2. ENode 2.0 - 介绍一下关于ENode中对Command的调度设计

    CQRS架构,C端的职责是处理从上层发送过来的command.对于单台机器来说,我们如何尽快的处理command呢?本文想通过不断提问和回答的方式,把我的思考写出来. 首先,我们最容易想到的是使用多线 ...

  3. 解析ASP.NET Mvc开发之EF延迟加载

    目录: 1)从明源动力到创新工场这一路走来 2)解析ASP.NET WebForm和Mvc开发的区别 3)解析ASP.NET Mvc开发之查询数据实例 ------------------------ ...

  4. 深入理解MVVM模式中Silverlight的Trigger、Action和Behavior及Silverlight的继承机制

    接触Silverlight已经有两三个月了,开始一直感觉他和Winform很相似,拖拖控件就行了,所以一直把经历放在了研究后台和服务器交互和性能优化上面,很少去仔细研究Silverlight的页面.前 ...

  5. 新版markdown功能发布!支持github flavored markdown!

    让大家久等了!新版markdown功能一直拖到今天才发布,很是愧疚...但不管怎么样,总算发布了! 今年1月份发布第一版markdown功能之后,很多园友反馈说做得很烂,我们综合大家的反馈之后发现不仅 ...

  6. onSingleTapUp()和onSingleTapConfirmed()的区别

    onSingleTapUp() - 在按下并抬起时发生,只要符合这个条件就触发该函数,没有任何附加条件. onSingleTapConfirmed() 同上者,但有附加条件,就是Android会确保单 ...

  7. make file教程(转)

    最近在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出 ...

  8. 搭建jekyll博客

    使用jekyll将markdown文件生成静态的html文件,并使用主题有序的进行布局,形成最终的博客页面. 特点 基于ruby 使用Markdown书写文章 无需数据库 可以使用GitHub Pag ...

  9. Atitit 项目的主体设计与结构文档 v3

    Atitit 项目的主体设计与结构文档 v3 1. 实现的目标2 1.1. cross device跨设备(pc 手机 平板)作为规划2 1.2. 企业级Java体系与开发语言2 1.3. 高扩展性, ...

  10. Netfilter/iptables的匹配方式及处理方法

    匹配方式: 匹配方式是netfilter筛选数据包的最基本单元. 内置的匹配方式: 1.接口的匹配方式: iptables -t filter -A FORWARD -i eth0 -o eth1 - ...