为什么 WCF 中不允许方法重载? [英] Why method overloading is not allowed in WCF?
问题描述
假设这是一个 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# 中存在的相同重载概念.以下帖子详细说明了为什么这是不可能的.
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
的 Name
属性.
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 中不允许方法重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!