C# 为什么 WCF 中不允许方法重载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10276124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why method overloading is not allowed in WCF?
提问by Sleiman Jneidi
Assume that this is a ServiceContract
假设这是一个 ServiceContract
[ServiceContract]
public interface MyService
{
[OperationContract]
int Sum(int x, int y);
[OperationContract]
int Sum(double x, double y);
}
Method overloading is allowed in C#, but WCF does not allow you to overload operation contractsThe hosting program will throw an InvalidOperationExceptionwhile hosting
C#中允许方法重载,但WCF不允许你重载operation contracts宿主程序会抛出一个InvalidOperationExceptionwhile宿主
采纳答案by David Z.
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.
简而言之,不能重载方法的原因与 WSDL 不支持 C# 中存在的相同重载概念有关。以下帖子详细说明了为什么这是不可能的。
http://jeffbarnes.net/blog/post/2006/09/21/Overloading-Methods-in-WCF.aspx
http://jeffbarnes.net/blog/post/2006/09/21/Overloading-Methods-in-WCF.aspx
To work around the issue, you can explicitly specify the Nameproperty of the OperationContract.
要解决这个问题,你可以明确地指定Name的属性OperationContract。
[ServiceContract]
public interface MyService
{
[OperationContract(Name="SumUsingInt")]
int Sum(int x, int y);
[OperationContract(Name="SumUsingDouble")]
int Sum(double x, double y);
}
回答by Wiktor Zychla
Because when invoking over HTTP/SOAP, having the same method name in your contractwould mean that there's no way to determine which particular method the client is about to invoke.
因为在通过 HTTP/SOAP 调用时,合同中具有相同的方法名称意味着无法确定客户端将要调用的特定方法。
Remember that when invoking web methods over http, arguments are optional and are initialized with default values if missing. This means that invocation of both methods could look exactly the same way over HTTP/SOAP.
请记住,通过 http 调用 Web 方法时,参数是可选的,如果缺少则使用默认值进行初始化。这意味着两种方法的调用在 HTTP/SOAP 上的调用方式可能完全相同。
回答by Baahubali
The reason being that WCF is supposed to be universally usable by different clients and those clients do not have to be .NET and may also not allow Operator/Method overloadingin their frameworks. If that is the case, then this method becomes unusable by those frameworks and therefore to ensure it is universally usable, WCF disallows and provides you with a name property in the OperationContractattribute to specify a different name. If the client is a .NET, then WCF will automatically show it as a method overload but otherwise it will generate a new method with the name specified in the name property.
原因是 WCF 应该被不同的客户端普遍使用,而这些客户端不必是 .NET,也可能不允许Operator/Method overloading在它们的框架中使用。如果是这种情况,那么这些框架将无法使用此方法,因此为确保它普遍可用,WCF 不允许并在OperationContract属性中提供名称属性以指定不同的名称。如果客户端是 .NET,则 WCF 将自动将其显示为方法重载,否则它将生成一个具有 name 属性中指定名称的新方法。

