WCF实现方法重载
一、服务契约(包括回调契约)通过指定不同的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();
}
}
这篇文章参考和引用如下作者的文章内容:
文章同步发表于我的个人网站:http://www.zuowenjun.cn/post/2015/03/25/135.html
WCF实现方法重载的更多相关文章
- WCF之操作重载
服务契约的方法重载,会在装载宿主时,抛出异常. 解决是在操作契约上Name设置为不同值,但是生成的代理会把Name的名称作为方法的名称,不过我们可以手动的修改代理类,使得方法名与服务声明的名称一样. ...
- java方法重载(overload)、重写(override);this、super关键简介
一.方法重载: 条件:必须在一个类中,方法名称相同,参数列表不同(包括:数据类型.顺序.个数),典型案例构 造方重载. 注意:与返回值无关 二.方法重写: 条件: (1)继承某个类或实现某接口 (2 ...
- Java之方法重载篇(我重载了,你要如何来调用我。。)
一.课前引言 请看一下代码,你发现什么特殊之处了吗? public class MethodOverload { public static void main(String[] args) { ...
- 【java开发】方法重写和方法重载概述
类的继承 父类-子类 关键字 extends 新建一个父类 public class Person { private String name; private int ...
- 方法构造和方法重载之奥特曼与大boss之战
知识点的总结: 1.类中的方法分为两类:1.普通方法: 2.构造方法. 2.构造方法的格式: public 类名(数据类型 参数名,...){ } 3.构造方法的用途: 1.实例化对象. 2. ...
- JavaScript中的方法重载
对js有些了解的人都知道,在js中根本就不存在像C#中的那种方法重载,而有的只是方法的覆盖,当你在js中敲入两个或多个同名的方法的时候,不管方法(函数)的参数个数怎么个不同,这个方法名只能属于最后定义 ...
- PHP面向对象编程——深入理解方法重载与方法覆盖(多态)
什么是多态? 多态(Polymorphism)按字面的意思就是“多种状态”.在面向对象语言中,接口的多种不同的实现方式即为多态.引用Charlie Calverts对多态的描述——多态性是允许你将父对 ...
- c#方法重载,可选参数,命名参数。
其实这里没什么可说哦,c++的语法大同小异.先看一段代码. class Program { public static void Test(int a) { Console.WriteLine(&qu ...
- java中的方法重载与重写以及方法修饰符
1. 方法重载Overloading , 是在一个类中,有多个方法,这些方法的名字相同,但是具有不同的参数列表,和返回值 重载的时候,方法名要一样,但是参数类型和参数个数不一样,返回值类型可以相同,也 ...
随机推荐
- tcp/udp高并发和高吐吞性能测试工具
在编写一个网络服务的时候都比较关心这个服务能达到多少并发连接,而在这连接的基础上又能达到一个怎样的交互能力.编写服务已经是一件很花力气的事情,而还要去编写一个能够体现结果的测试工具就更加消耗工作时间. ...
- 在ASP.NET WebAPI 中使用缓存【Redis】
初步看了下CacheCow与OutputCache,感觉还是CacheOutput比较符合自己的要求,使用也很简单 PM>Install-Package Strathweb.CacheOutpu ...
- 我的ORM之十三 -- 性能参数
我的ORM索引 测试环境 台式机: 主板:映泰Z77 CPU:i5 3470(3.2GHz) 内存:DDR3 1600 8G(单条) 硬盘:创见 SSD 256G ORM从过程上,可以分两个大的部分: ...
- C# 关于Try/Catch对系统性能影响的总结
自从开始考虑代码的运行效率和性能以后,写代码考虑的东西越来越多了,比如什么时候应该加try/catch?加太多的try/catch会不会降低性能?今天就来分享一下对try/catch对性能影响的一些看 ...
- 十进制数转化成二进制后包含一的数量(c++)
#include <iostream> using namespace std;int func(int x){ int count=0; while(x){ ...
- C#实现的等额本息法、按月付息到期还本法、一次性还本付息法
你若懂行,那便有用,如下: void Main(){ var x = DengEBenXi.Compute(11111, 12, 3); x.Dump(); var y = AnYu ...
- [Unity3D]再次点击以退出程序
[Unity3D]再次点击以退出程序 本文介绍为Android应用编写点击返回按键时的"再次点击以退出程序"的方法. +BIT祝威+悄悄在此留下版了个权的信息说: 下面是一个测试用 ...
- Android学习笔记50:使用WebView控件浏览网页
在Android中,可以使用Webview控件来浏览网页.通过使用该控件,我们可以自制一个简单的浏览器,运行效果如图1所示. 图1 运行效果 1.WebView 在使用WebView控件时,首先需要在 ...
- ios UIView sizeToFit sizeThatFits
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 0, 0)]; testLabel.backgroundC ...
- Vue.js2.0从入门到放弃---入门实例
最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...