假设这是一个ServiceContract
[ServiceContract]
public interface MyService
{
[OperationContract]
int Sum(int x, int y);
[OperationContract]
int Sum(double x, double y);
}
C#中允许方法重载,但WCF不允许重载operation contracts
托管程序将在托管时抛出 InvalidOperationException
最佳答案
简而言之,您不能重载方法的原因与 WSDL 不支持 C# 中存在的相同重载概念这一事实有关。以下帖子详细说明了为什么这是不可能的。
http://jeffbarnes.net/blog/post/2006/09/21/Overloading-Methods-in-WCF.aspx
要解决此问题,您可以显式指定 OperationContract
的 Name
属性。
[ServiceContract]
public interface MyService
{
[OperationContract(Name="SumUsingInt")]
int Sum(int x, int y);
[OperationContract(Name="SumUsingDouble")]
int Sum(double x, double y);
}
关于c# - 为什么 WCF 中不允许方法重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10276124/