问题描述
假设这是一个的ServiceContract
[ServiceContract]
public interface MyService
{
[OperationContract]
int Sum(int x, int y);
[OperationContract]
int Sum(double x, double y);
}
方法重载允许在C#中,但WCF不允许超载营运合约
该托管程序将抛出一个 InvalidOperationException异常
,而托管
Method overloading is allowed in C#, but WCF does not allow you to overload operation contracts
The hosting program will throw an InvalidOperationException
while hosting
推荐答案
在简单地说,你不能重载方法的原因,是因为有一个事实,即WSDL不支持C#里面同样重载的概念present。下面交提供为什么这是不可能的细节。
In a nutshell, the reason you cannot overload methods has to do with the fact that WSDL does not support the same overloading concepts present inside of C#. The following post provides details on why this is not possible.
http://jeffbarnes.net/blog/post/2006/09/21/Overloading-Methods-in-WCF.aspx
要解决这个问题,你可以明确指定的名称
属性 OperationContract的
。
To work around the issue, you can explicitly specify the Name
property of the OperationContract
.
[ServiceContract]
public interface MyService
{
[OperationContract(Name="SumUsingInt")]
int Sum(int x, int y);
[OperationContract(Name="SumUsingDouble")]
int Sum(double x, double y);
}
这篇关于为什么方法重载没有在WCF允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!