如果您懒得看下面的文字,您按下面的提示下载视频教程,里面还有ppt和源代码
本文目的:
阅读本文,您能了解以下知识
- 什么是操作重载?操作重载有什么好处
- WCF的服务端如何解决操作重载的问题?
- WCF的客户端如何解决操作重载问题?
- 小结
什么是操作重载?操作重载有什么好处
重载指的是在同一个类,接口或者结构中包含多个同名的 方法,而这些方法的参数列表或者返回值各不相同.使用它的好处在于提高模型的强壮性和通用性,使模型在一个可维护统一高度上运行,其功能和返回依赖于传递的参数. 在传统的程序开发中,我们程序员经常使用这种技术,比如一个有一个功能既能够计算两个整数的和,又能计算两个双精度数的和,这样的需求,我们往往会按下面这样书写代码:
public
int
Add(
int
a,
int
b)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return a + b;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
double
Add(
double
a,
double
b)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return a + b;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
而在WCF中,还能不能这么干呢?不能!为什么呢?WCF中无论是服务端还是客户端,如果单拿出来一个都是支持操作重载的,但是客户端代理生成的依据却是WSDL,而WSDL是不支持操作重载的,另外客户端调用服务端的一个操作的必须先要确定两个要素:1)操作所属的服务是哪一个?2) 操作在服务中的名称是什么?这样的话,向传统应用程序程序那样重载就会出现问题!
WCF的服务端如何解决操作重载的问题?
如果按照下面的代码来实现一个服务:
服务契约
[ServiceContract]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
interface
IService
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[OperationContract]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int Add(int a, int b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[OperationContract]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
double Add(double a, double b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
而服务实现为:
public
class
Service : IService
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Add(int a, int b)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return a + b;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public double Add(double a, double b)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return a + b;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
那么,在编译的时候,是没有错误的。此时,我们再按照下面的代码实现一个托管:
using
(ServiceHost host
=
new
ServiceHost(
typeof
(Service),
new
Uri(
"
net.tcp://127.0.0.1:12345
"
)))
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NetTcpBinding bind = new NetTcpBinding();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
host.AddServiceEndpoint(typeof(IService), bind, "");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//下面代码的目的是添加一个MeatedataExchage的EndPoint
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
BindingElement bindElement = new TcpTransportBindingElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CustomBinding metaBind = new CustomBinding(bindElement);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ServiceMetadataBehavior metaBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (metaBehavior == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
metaBehavior = new ServiceMetadataBehavior();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
host.Description.Behaviors.Add(metaBehavior);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
host.AddServiceEndpoint(typeof(IMetadataExchange), metaBind, "MEX");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
host.Open();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("服务已经运行!");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Read();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
随后,我们启动托管程序,发现代码在运行到ServiceHost host = new ServiceHost(typeof(Service),new Uri("net.tcp://127.0.0.1:12345"))的时候,发生如下的异常:
这个异常提示我们,同一个协定之中不能存在相同的操作,在WCF中操作重载是不显示适用的。而且问题不在于编译阶段,而在于托管阶段。
但是我们能通过一些改进的手段来获取WCF对操作重载的支持。我们先把服务契约的定义更改为如下的代码:
[ServiceContract]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
interface
IService
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[OperationContract(Name="AddInt")]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int Add(int a, int b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[OperationContract(Name="AddDouble")]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
double Add(double a, double b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
此时,我们在启动托管,发现已经能够正常运行了
到此,我们是不是就完美的解决了WCF中关于操作重载的问题呢?不,还没有,因为WCF既包含服务端,又包含客户端,我们当前已经将服务端顺利的运行起来了。总结一下,就说服务端不是显示支持重载的,重载的操作各自的别名必须更不一致。
WCF的客户端如何解决操作重载问题?
但客户端呢?下面就来看下客户端对操作重载的反应。
要想实现客户端,我们创建一个Console的客户端应用程序,然后需要用SvcUtiil.exe生成代理类,方法如下![](https://i-blog.csdnimg.cn/blog_migrate/b92d566c067b7d4cc771da9931cc2181.png)
打开Proxy.cs,我们会发现代理的代码如下:
默认生成的代理类代码
//------------------------------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// <auto-generated>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 此代码由工具生成。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 运行库版本:2.0.50727.1433
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 对此文件的更改可能会导致不正确的行为,并且如果
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 重新生成代码,这些更改将会丢失。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// </auto-generated>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//------------------------------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IService")]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public interface IService
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService/AddInt", ReplyAction="http://tempuri.org/IService/AddIntResponse")]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int AddInt(int a, int b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService/AddDouble", ReplyAction="http://tempuri.org/IService/AddDoubleResponse")]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
double AddDouble(double a, double b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public interface IServiceChannel : IService, System.ServiceModel.IClientChannel
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[System.Diagnostics.DebuggerStepThroughAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class ServiceClient : System.ServiceModel.ClientBase<IService>, IService
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ServiceClient()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ServiceClient(string endpointConfigurationName) :
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base(endpointConfigurationName)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ServiceClient(string endpointConfigurationName, string remoteAddress) :
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base(endpointConfigurationName, remoteAddress)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base(endpointConfigurationName, remoteAddress)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base(binding, remoteAddress)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int AddInt(int a, int b)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return base.Channel.AddInt(a, b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public double AddDouble(double a, double b)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return base.Channel.AddDouble(a, b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
仔细观察代理的代码不难发现,代理中的服务契约以及服务实现的操作与服务端定义的想比,有所更改。而且对于代理类来说,已经没有了重载,虽然此代理类能够被正常使用,但是却没有了重载的好处,如何更改代理类,才能使其也有重载是下面要研究的问题。
我们将代理中的服务契约IService中的操作定义修改为:
[System.ServiceModel.OperationContractAttribute(Action
=
"
http://tempuri.org/IService/AddInt
"
, ReplyAction
=
"
http://tempuri.org/IService/AddIntResponse
"
,Name
=
"
AddInt
"
)]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int
Add(
int
a,
int
b);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[System.ServiceModel.OperationContractAttribute(Action
=
"
http://tempuri.org/IService/AddDouble
"
, ReplyAction
=
"
http://tempuri.org/IService/AddDoubleResponse
"
,Name
=
"
AddDouble
"
)]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
double
Add(
double
a,
double
b);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
然后将服务实现ServiceClient中的方法更改为:
public
int
Add(
int
a,
int
b)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return base.Channel.Add(a, b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
double
Add(
double
a,
double
b)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return base.Channel.Add(a, b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
创建一个Console的客户端应用程序,然后将修改后的Proxy.cs拷贝到其中,实现客户端调用,代码如下:
IService ws
=
new
ServiceClient(
new
NetTcpBinding(),
new
EndpointAddress(
"
net.tcp://127.0.0.1:12345
"
));
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using
(ws
as
IDisposable)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(ws.Add(1,2).ToString());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(ws.Add(1.3, 2.4).ToString());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Console.Read();
将解决方案设置为多启动项目,并启动托管和客户端,出现下面的结果:
说明客户端和服务端已经成功通讯。从上面实现客户端的方法来看,客户端想实现重载,也必须保证重载操作的别名要有服务端的相匹配,其各不相同。
小结
从本文可以看出,WCF编程虽然保持了大部分原有编程模式,也继承了原有模式足够多好的做法,但是限于分布式开发与传统应用程序的差异,在有些细节上还是有区别的,比如本文所讨论的操作重载问题,其实还有很多类似问题,比如继承的差异,序列化的差异,处理集合的差异等等。要想真正的掌握这些问题,必须要深刻了解分布式开发的特型。加深对服务交互,类型转换,封送等WCF架构方面的理解。以下几点是对本文的总结:
1) 对于WCF中的服务端,对服务契约和服务实现不支持显示的操作重载,但可以通过设置重载操作的别名来改善这种状况
2) 对于WCF客户端,默认情况下,生成的代理也不支持操作重载,想要改变这种状况也必须依赖于别名。
3) 我推荐的做法是在服务端还是要用别名的方式支持操作重载,在客户端手动更改代理类,以便也支持操作重载。
视频,课件以及源码下载