WCF学习笔记之重载
来源:互联网 发布:linux 网络服务器 编辑:程序博客网 时间:2024/06/05 01:59
转载自 http://www.cnblogs.com/xyz168/archive/2011/11/15/2249505.html
本节内容:1.什么是重载?2.WCF服务端处理重载。3.WCF客户端处理重载
一、什么是重载?
重载指的是在同一个类,接口或结构中包含多个同名的方法,而这些方法的参数列表或返回值不同。正常的做法如下所示:
View Code
1 public string Say(string lastName)2 { 3 return "Hello " + lastName;4 }5 6 public string Say(string firstName, string lastName)7 {8 return "Hello " + firstName + "" + lastName;9 }
在WCF中的重载是否不一样呢?
二、WCF服务端的处理重载。
在WCF中需要为服务操作指定唯一名称,需要用到:OperationContract的Name属性如下所示,如果去掉Name属性,会有异常,可以自己动手实验一下。
契约:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ServiceModel; 7 namespace y.WcfServiceContract.OverloadHost.Services 8 { 9 [ServiceContract]10 public interface IHello11 {12 [OperationContract(Name="SayLastName")]13 string Say(string lastName);14 15 [OperationContract(Name = "SayFullName")]16 string Say(string firstName, string lastName);17 }18 }
服务类:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ServiceModel; 7 namespace y.WcfServiceContract.OverloadHost.Services 8 { 9 [ServiceBehavior]10 public class HelloService:IHello11 {12 [OperationBehavior]13 public string Say(string lastName)14 {15 Console.WriteLine("Riceive from client :{0}", lastName);16 return "Hello " + lastName;17 }18 19 [OperationBehavior]20 public string Say(string firstName, string lastName)21 {22 Console.WriteLine("Riceive from client:{0},{1}", firstName, lastName);23 return "Hello " + firstName + "" + lastName;24 }25 }26 }
允许客户端以元数据的形式的获取代理类,对其配置文件如下:
View Code
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <behaviors> 5 <serviceBehaviors> 6 <behavior name="metadataBehavior"> 7 <serviceMetadata /> 8 </behavior> 9 </serviceBehaviors>10 </behaviors>11 <services>12 <service name="y.WcfServiceContract.OverloadHost.Services.HelloService" behaviorConfiguration="metadataBehavior">13 <host>14 <baseAddresses>15 <add baseAddress="net.tcp://localHost:6666/"/>16 </baseAddresses>17 </host>18 <endpoint address="Hello"19 binding="netTcpBinding"20 contract="y.WcfServiceContract.OverloadHost.Services.IHello"></endpoint>21 <endpoint address="MEX"22 binding="mexTcpBinding"23 contract="IMetadataExchange"></endpoint>24 </service>25 </services>26 </system.serviceModel>27 </configuration>
三、WCF客户端处理重载
以两种方式创建代理类看看重载的不同:
1.以元数据方式获取的代理类,产生的Contract 和ClientProxy代码如下:
契约:
View Code
1 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] 2 [System.ServiceModel.ServiceContractAttribute(ConfigurationName="MetadataProxys.IHello")] 3 public interface IHello { 4 5 [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IHello/SayLastName", ReplyAction="http://tempuri.org/IHello/SayLastNameResponse")] 6 string SayLastName(string lastName); 7 8 [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IHello/SayFullName", ReplyAction="http://tempuri.org/IHello/SayFullNameResponse")] 9 string SayFullName(string firstName, string lastName);10 }
ClientProxy:
View Code
1 [System.Diagnostics.DebuggerStepThroughAttribute()] 2 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] 3 public partial class HelloClient : System.ServiceModel.ClientBase<y.WcfServiceContract.OverloadWinClient.MetadataProxys.IHello>, y.WcfServiceContract.OverloadWinClient.MetadataProxys.IHello { 4 5 public HelloClient() { 6 } 7 8 public HelloClient(string endpointConfigurationName) : 9 base(endpointConfigurationName) {10 }11 12 public HelloClient(string endpointConfigurationName, string remoteAddress) : 13 base(endpointConfigurationName, remoteAddress) {14 }15 16 public HelloClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 17 base(endpointConfigurationName, remoteAddress) {18 }19 20 public HelloClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 21 base(binding, remoteAddress) {22 }23 24 public string SayLastName(string lastName) {25 return base.Channel.SayLastName(lastName);26 }27 28 public string SayFullName(string firstName, string lastName) {29 return base.Channel.SayFullName(firstName, lastName);30 }31 }
比较WCF服务端的契约可以看出。现在的代理类方法名称均为,WCF服务中OperationContract指定的Name名称。
2.手动编写代理类,实现重载。
契约,和服务端相同,包括OpertaionContract的Name属性,如下:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ServiceModel; 7 namespace y.WcfServiceContract.OverloadWinClient.ManualProxy 8 { 9 [ServiceContract]10 public interface IHello11 {12 [OperationContract(Name = "SayLastName")]13 string Say(string lastName);14 15 [OperationContract(Name = "SayFullName")]16 string Say(string firstName, string lastName);17 }18 }
代理类的实现:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ServiceModel; 7 using System.ServiceModel.Channels; 8 namespace y.WcfServiceContract.OverloadWinClient.ManualProxy 9 {10 public class HelloProxy:ClientBase<IHello>,IHello11 {12 public HelloProxy(string endpointCon)13 : base(endpointCon)14 {15 }16 17 public string Say(string lastName)18 {19 return base.Channel.Say(lastName);20 }21 22 public string Say(string firstName, string lastName)23 {24 return base.Channel.Say(firstName, lastName);25 }26 }27 }
运行服务端,客户端获取的结果如下,说明通信正常。
- WCF学习笔记之重载
- silverlight之wcf双工通讯学习笔记
- WCF 学习笔记之双工实现
- WCF 学习笔记之异常处理
- WCF 学习笔记之绑定(Binding)
- SPL学习笔记之四:数组重载
- C++学习笔记之运算符重载
- C++学习笔记之操作符重载
- PHP学习笔记之:函数重载
- C++之操作符重载学习笔记
- swift学习笔记之运算符重载
- C++学习笔记之函数重载简介
- C++之操作符重载学习笔记
- C++学习笔记十二之函数重载
- C#学习笔记之——重载
- c++学习笔记之运算符重载
- [WCF学习笔记] Endpoint
- [WCF学习笔记] Hosting
- 嵌入式学习之路(四)——使用vi编写c程序
- 【代码】ArcGIS For Flex 由地理坐标转换成自定义投影坐标系统参考
- cocos2c-x技能cd
- poj 3281 最大流
- Java 内存溢出(java.lang.OutOfMemoryError)问题总结
- WCF学习笔记之重载
- UML类图几种关系的总结
- myEclipse安装和使用
- [LeetCode]104.Maximum Depth of Binary Tree
- css+div
- Socket通信中的多进程编程实例
- Linux系统启动的标准流程
- python 核心模块之pickle和cPickle讲解
- 笔记7 (复制控制)